Dead Pool
Dead Pool

Reputation: 133

Variables in Django template are showing in double quotes

I fetch the data from my Post model and simply passed into the template. But the output is showing in double quotes. I have html tags in my Post model, which is not rendering but showing as codes.

views.py

# It display the single post
def single_post(request,cat,post_id,post_title):
    single_post = Post.objects.get(pk = post_id)
    return render(request, 'blog/single_post.html', {'single_post': single_post})

single_post.html

<h1>I'm single post page</h1>

{{single_post.title}}

<br>
<br>
{{single_post.content}}

Browser Output

enter image description here enter image description here

Data in my Database enter image description here

Upvotes: 4

Views: 3259

Answers (1)

shacker
shacker

Reputation: 15371

In your screenshot, I do not see double quotes, I see HTML tags. If you are entering HTML into the content, rather than letting untrusted users enter it, then you should consider it safe for rendering onto the page (you're not likely to post malicious javascript, or to screw up your layout with bad HTML, right?). Therefore you want to use Django's safe filter on your content:

{{single_post.content|safe}}

That will allow the entered and stored HTML to be interpreted, rather than rendered.

Upvotes: 6

Related Questions