2

I'm trying to set up a Django app that has a MarkupField in it's model, like this:

from django.db import models
from markupfield.fields import MarkupField

class Recipe(models.Model):
    instructions = MarkupField(default_markup_type='markdown')

Then I render the field in a Jinja2 template, like this:

{% if recipe.instructions %}
    {{ recipe.instructions }}
{% else %}
    No instructions have been added yet.
{% endif %}

Rendering of the markdowned text works flawless, but it is placed as a string inside the DOM so the browser doesn't interpret the HTML tags, like you can see here:

View in the browser, note the <p> tag.

View in the DOM inspector, also note the <p> tags.

I don't feel like I missed something relevant in the django-markupfield's docs, but somehow I need to get rid of this string representation.

Anyone of you guys got an idea? Thanks in advance.

4
  • 1
    If you add the safe filter does it works: {{ recipe.instructions|safe }}? Commented Feb 5, 2016 at 19:50
  • @doru Nope, doesn't work. Commented Feb 5, 2016 at 20:00
  • If you use recipe.instructions.rendered does it make any change? Commented Feb 5, 2016 at 20:14
  • 1
    @doru Doesn't work either. I managed to get it working by {% autoescape off %}{{ recipe.instructions }}{% endautoescape %} like suggested in this question. Find it to be a bit inconvenient though... Commented Feb 5, 2016 at 20:26

1 Answer 1

0

Thanks to @doru's advices I stumbled across the Jinja2 documentation and found the autoescaping statement:

{% autoescape off %}{{ recipe.instructions }}{% endautoescape %}

This one worked for me.

It's even possible to make it work globally by setting the autoescape option to False.

{
    'BACKEND': 'django_jinja.backend.Jinja2',
    ...
    'OPTIONS': {
        'autoescape': False,
        ...
    }
},
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.