5

I want to save html code in database through django.

In models.py i have this field

description = models.TextField()

But when i save data in description field through Django Admin. It display data like this

<h1>This is example</h1>

rather then converting it in html. Here's the Out Put

1 Answer 1

12

Template engine escaping html default. You have to filter it with safe tag when you showing.

{{ description |safe }}

or use autoescape tag

{% autoescape off %}{{ description }}{% endautoescape %}
Sign up to request clarification or add additional context in comments.

5 Comments

where can i use these tags ? in view.py or in models.py
or in my view.html
It should be in your view.
Thanks its working..i just put this code in view.html like this {% autoescape off %}{{object.description | linebreaks }}{% endautoescape %}
@devdap You need to be careful when you say it should be in your view. View in Django is equivalent to Controllers in other frameworks. So I would say "it should be in your template i.e. template.html"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.