HTML Django 文本字段模型上的格式
HTML format on Django text field model
我想创建一个博客。我想在管理面板中创建博客内容并以 HTML 格式呈现博客内容。
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
在管理面板中:
Create blog content with HTML format in admin
在模板中:
{% extends 'base.html' %}
{% block content %}
<h1>{{ blog.title }}</h1>
<p>{{ blog.content }}</p>
{% endblock content %}
是否有任何模板标签、模型字段或任何东西可以做到这一点?
您只需使用内置的 safe
模板标签即可。
在您的模板中:
{% extends 'base.html' %}
{% block content %}
<h1>{{ blog.title }}</h1>
<p>{{ blog.content | safe }}</p>
{% endblock content %}
在文档中查看相同的 here。
我想创建一个博客。我想在管理面板中创建博客内容并以 HTML 格式呈现博客内容。
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
在管理面板中: Create blog content with HTML format in admin
在模板中:
{% extends 'base.html' %}
{% block content %}
<h1>{{ blog.title }}</h1>
<p>{{ blog.content }}</p>
{% endblock content %}
是否有任何模板标签、模型字段或任何东西可以做到这一点?
您只需使用内置的 safe
模板标签即可。
在您的模板中:
{% extends 'base.html' %}
{% block content %}
<h1>{{ blog.title }}</h1>
<p>{{ blog.content | safe }}</p>
{% endblock content %}
在文档中查看相同的 here。