-2

Hi i am practicing Django and Python

And i encounter with parse HTML problem, i saved some html through admin page, and i tried to render,but it rendered as string!
enter image description here

This is my part of template

description <- columns is my problem

other columns are rendered well

{% block content %}
<div class="article-detail">
    <div class="article">
        <img src="{{ article.thumb.url }}" />
        <h2>{{ article.title }}</h2>
        <p>{{ article.date }}</p>
        <div>
{{ article.description }}
        </div>
    </div>
</div>

{% endblock %}

And this is my model

from tinymce import HTMLField

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField()
    body = models.TextField()
    description = HTMLField('Content', blank=True)
    body = models.TextField()
    date = models.DateTimeField(auto_now_add=True)
    thumb = models.ImageField(default='default.png', blank=True)
    ...

This part of my view

def article_detail(request, slug):
    article = Article.objects.get(slug=slug)
    return render(request, 'articles/article_detail.html', {'article': article})

I tried to use javascript

{% block content %}
<div class="article-detail">
    <div class="article">
        <img src="{{ article.thumb.url }}" />
        <h2>{{ article.title }}</h2>
        <p>{{ article.date }}</p>
    </div>
</div>

<script>
    var article = "<div>{{ article.description }}</div>"
    console.log(article);
    document.write(article);
</script>
{% endblock %}

I hoped this work but
But i got JS syntax Error
console dosen't show anything
Why? If it transformed to string, it should work....

And what is best way to render it ?

And i am sorry for my poor English

xyres
  • 20,487
  • 3
  • 56
  • 85
KimMinJae
  • 135
  • 4
  • 10

4 Answers4

1

By default, Django auto-escapes output from the template variables in order to avoid Cross-site scripting. If you want to render unescaped string then you can either use safe filter or autoescape template block as follows:

{{ var|safe }}

OR

{% autoescape off %}
    {{ var }}
{% endautoescape %}
Sijan Bhandari
  • 2,941
  • 3
  • 23
  • 36
1

Django's templating engine does escaping automatically, so you don't really need to escape.

If you add template filter "safe" like {{article.description|safe}} then you do need to worry about things like html injection, because "safe" marks the string as such and it means that it won't be escaped.

There is also an {% autoescape on %}...{% endautoescape %} template tag, where "on" can be changed to "off", if necessary. By default it's on and the tag is not needed.

Other template engines may not be escaping by default, Jinja2 is one of them.

Enoch Chejieh
  • 136
  • 1
  • 4
0

Add the safe filter to your html template var. Django automatically escapes html to prevent html injection. You can ‘turn this off’ by adding |safe. For example:

<div> {{ article.description|safe }} </div>

Vincent
  • 1,494
  • 12
  • 26
0

The Django template processors are escaping the HTML syntax inside your model.I think you need to include this header tag in order to make your tinyMCE app work. It is some javascript code which probably renders the HTMLField.

<head>
    ...
    {{ form.media }}
</head>

Reference

spaceSentinel
  • 630
  • 6
  • 9