【发布时间】:2018-06-23 17:58:55
【问题描述】:
我有一个带有 Post 对象的 Django 站点,如下所示:
class Post(models.Model):
title = models.CharField(max_length=100,blank=True,null=True)
body = models.TextField(blank=True,null=True)
author = models.ForeignKey(User,blank=True,null=True)
date_created = models.DateTimeField(default=timezone.now)
date_updated = models.DateTimeField(auto_now_add=True)
image = models.ImageField(upload_to=post_dir, blank=True, null=True)
def __unicode__(self):
return unicode(self.date_created.strftime('%Y-%m-%d %H:%M') + ' ' + self.title)
像这样输出body TextField以支持HTML:
{% if post.body %}
<p>
{{ post.body | safe }}
</p>
{% endif %}
我的问题是,由于管理员可以输入可能导致 html 格式错误的 HTML(例如 post.body = '</div></div>'),那么在仍然允许用户输入 html 的同时格式化和清理此文本字段的最佳方法是什么?
【问题讨论】:
-
您正在寻找的是由 Django CKEditor 提供的 RichTextField - 请参阅 github.com/django-ckeditor/django-ckeditor
-
很酷,谢谢!我现在正在检查它,它看起来很不错。
标签: html django content-management-system