Search code examples
htmldjangosummernote

Django: Why does the text that I placed in Django Summernote displays HTML tags in my HTML template?


I have installed Django Summernote by following the guide from GitHub and have successfully added it to my admin page. However, when I try to edit the text using summernote and view it on the HTML template, it shows the code view instead of the text only. I'm new to Django so I'm not sure what I'm missing here.

My admin page where summernote is applied:

admin page

My html page where the above would be viewed:

html page

admin.py:

from django.contrib import admin
from django_summernote.admin import SummernoteModelAdmin

from .models import Post

class PostAdmin(SummernoteModelAdmin):
    summernote_fields = ('body',)

admin.site.register(Post, PostAdmin)

models.py:

from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
    ...

    body = models.TextField()

    ...

    def __str__(self):
        return self.title

EDIT: Where I display the "body" in my HTML template:

{% block body %}   

    ...

    <p>{{ post.body }}</p>

    ...

{% endblock %}

Solution

  • You will need to use safe filter here.

    Safe Marks a string as not requiring further HTML escaping prior to output. When autoescaping is off, this filter has no effect.

    Your edit:

    <p>{{ post.body | safe }}</p>