3

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Avoid using the "safe" filter unless absolutely necessary. The "safe" filter disables automatic HTML escaping, which can lead to cross-site scripting (XSS) attacks if untrusted data is used in a template.
1

You have some options.

  1. You can use a Content Management System (CMS) like django CMS or Wagtail.

  2. If you don't want to add the complexity of having a full CMS, you can add a simple rich text editor (WYSIWYG editor) like CKEditor or tiny.

  3. Avoid using the "safe" filter unless absolutely necessary. The "safe" filter disables automatic HTML escaping, which can lead to cross-site scripting (XSS) attacks if untrusted data is used in a template.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.