Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML format on Django text field model

I want to create a blog. I want to create blog content in admin panel and render the blog content with HTML format.

models.py

class Blog(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    date_created = models.DateField(auto_now_add=True)

def __str__(self):
    return self.title

In admin panel : Create blog content with HTML format in admin

In template:

{% extends 'base.html' %}

{% block content %}
<h1>{{ blog.title }}</h1>
<p>{{ blog.content }}</p>
{% endblock content %}

Is there any template tags, model field, or anything to do this?

like image 907
penk Avatar asked May 12 '26 13:05

penk


1 Answers

You can do this by simply using the built in safe template tag.

In your template:

{% extends 'base.html' %}

{% block content %}
<h1>{{ blog.title }}</h1>
<p>{{ blog.content | safe }}</p>
{% endblock content %}

check the docs for the same here.

like image 97
Hussain Pettiwala Avatar answered May 15 '26 02:05

Hussain Pettiwala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!