Ryan Oh
Ryan Oh

Reputation: 657

How do I bring full html code from Django database to template?

so I have a full html code-from !Doctype to -stored in my Django database. I was hoping I could bring them as template, NOT as the raw content displayed on the screen. Here's my models.py

class NewsLetter(models.Model):
    title = CharField(max_length=10)
    content = TextField()

And in the content field, I have stored full html code. In my views.py,

from .models import NewsLetter

def letter(request, id):
    thisLetter = NewsLetter.objects.get(pk=id)
    return render(request, 'letter.html', {'thisLetter' : thisLetter}

and finally in my letter.html,

{{thisLetter.content}}

I do understand why it displays the full html code, not the content. But I have no idea on how to display content instead of raw html code.
I very much appreciate your help :)

Upvotes: 0

Views: 29

Answers (1)

michaeldel
michaeldel

Reputation: 2385

Use the safe template flag

{{ thisLetter.content | safe }}

Ensure you really trust the HTML stored in your database though, otherwise you might run into nasty security issues (that's why HTML escaping is enabled by default).

Upvotes: 1

Related Questions