Vinsmoke Mau
Vinsmoke Mau

Reputation: 347

Django-CKeditor: How to show RichTextFields in templates

The problem is that I want to show the content of a Post in the template but I don't know how

The model of Post is:

from ckeditor.fields import RichTextField

class Post(models.Model):

    ...
    content = RichTextField(verbose_name='contenido')
    ...

And in the template I have a for to show all the post that is like this:

{% for post in posts %}
    ...
    {{ post.content }}
    ...
{% endfor %}

But when I see the page in the browser shows this: < p > Post content < /p >

Instead of this: Post content

Upvotes: 3

Views: 5969

Answers (2)

Gata
Gata

Reputation: 465

{{article.body|safe}}

or

{{article.body|truncatechars:150|safe}}

or

{{article.body|truncatewords:25|safe}}

Upvotes: 1

tdsymonds
tdsymonds

Reputation: 1709

You need to mark the content as safe. So change your template to:

{% for post in posts %}
    ...
    {{ post.content|safe }}
    ...
{% endfor %}

By default HTML is not escaped and so is displayed as text, which is why you're seeing the <p> tags. You need to mark the field as safe so that Django renders it as HTML. See the documentation for more info.

Upvotes: 15

Related Questions