Displaying markdown in html

157 views
Skip to first unread message

Emily Namugaanyi

unread,
May 4, 2012, 8:55:28 AM5/4/12
to Django users
I am trying to display markdown in my html but it is returning a body
with html tags:
This is what is happening...

code in the

<!-- This is the template -->


<div class="tab-pane fade" id="guidelines">

{% for guideline in guidelines %}
{% load markup %}
<b> Question:</b> {{ guideline.question }} <br/>
<b> Answer:</b> {{ guideline.answer_markdown }} <br/
>
{% endfor %}
</div>


<!-- -->
<!-- This is the model -->

class Guideline(models.Model):
question = models.CharField(max_length=70, blank=True)
answer = models.TextField(max_length=500, blank=True)
answer_markdown = models.TextField(max_length=500, blank=True,
null=True )

def __unicode__(self):
return self.question

def save(self):
self.answer = markdown.markdown(self.answer_markdown)
super(Guideline, self).save()

<!-- -->

This is what is displayed in the browser;

Answer: <ol> <li>Nothing</li> <li>Alot</li> <li>Something</li>
<li>The sky</li> </ol>

<!-- This is what is expected-->
Answer: 1. Nothing
2. Alot
3. Something
4. The sky

<!-- -->

Tom Evans

unread,
May 4, 2012, 9:06:59 AM5/4/12
to django...@googlegroups.com
On Fri, May 4, 2012 at 1:55 PM, Emily Namugaanyi <enem...@gmail.com> wrote:
> I am trying to display markdown in my html but it is returning a body
> with html tags:
> This is what is happening...
>
> code in the
>
> <!-- This is the template -->
>
>
> <div class="tab-pane fade" id="guidelines">
>
>  {% for guideline in guidelines %}
>                    {% load markup %}
>                  <b>  Question:</b> {{ guideline.question }} <br/>
>                  <b>  Answer:</b> {{ guideline.answer_markdown }} <br/

When outputting content that contains HTML, and you want the HTML to
be rendered, not escaped, you either need to turn off escaping, or
mark the content as safe to output:

https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#safe

Cheers

Tom

creecode

unread,
May 4, 2012, 11:24:29 AM5/4/12
to django...@googlegroups.com
Hello Emily,

This is not a direct answer to your question.  It looks like Tom has your answer.  You will most likely want to move the {% load markup %} to near to top of your template.  Generally loads only needs to be done once at the start of the template.


On Friday, May 4, 2012 5:55:28 AM UTC-7, Emily Namugaanyi wrote:

<!-- This is the template -->

<div class="tab-pane fade" id="guidelines">

  {% for guideline in guidelines %}
                    {% load markup %}
                  <b>  Question:</b> {{ guideline.question }} <br/>
                  <b>  Answer:</b> {{ guideline.answer_markdown }} <br/
>
                {% endfor %}
</div>

Toodle-looooooooo.................
creecode

su

unread,
May 5, 2012, 12:34:14 AM5/5/12
to django...@googlegroups.com
#model
class Article(models.Model):
    udate=models.DateTimeField(auto_now=True,auto_now_add=True)
    markdown_content=models.TextField()
    html_content=models.TextField(editable=False)
    def save(self):
        self.html_content=markdown(self.markdown_content)
        self.update=datetime.datetime.now()
        super(Article,self).save()
#template
<div class="text">
        {{ article.html_content|safe}}
        <span>Update:{{article.update }}</span>
</div> 

su

unread,
May 5, 2012, 12:37:34 AM5/5/12
to django...@googlegroups.com
doc:disable auto-escaping

This will not be escaped: {{ data|safe }}

Emily

unread,
May 5, 2012, 10:08:51 AM5/5/12
to django...@googlegroups.com
Thank so much it worked.
Emily
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/p6ckOCJjQGwJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply all
Reply to author
Forward
0 new messages