简体   繁体   中英

How do I html format my blog post in django?

So I want to make a blog post with HTML formatting from the admin page directly. For example from the models.py, you see the description is a TextField. When I go to the admin page to make a blog post, I want to be able to add HTML tags to my blog. So the text field will have HTML content. While I call the blog post onto the HTML template I want it to read the description as a HTML file and not just a text field.

models.py

from django.db import models

class Blog(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    date = models.DateField()

Blog.description at admin page

<HTML>
<body>
<p>Welcome to my first blog post!!</p>
</body>
</html>

blog.html

  <h1>{{ blog.title }}</h1>
  <hr>
  <p>{{ blog.description }}</p>

Any help is appreciated. Thanks!

You can render it with the |safe template filter [Django-doc] . This will disable escaping HTML fragments, and it will thus no longer convert < to &lt; for example:

<h1>{{ blog.title }}</h1>
  <hr>
  <p>{{ blog.description }}</p>

You however might want to take a look at the django-ckeditor package [GitHub] which offers a dedicated field, widget, etc. to enable editing the text with respect to rendering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM