我在我的model.py 中添加了 trix
from trix.fields import TrixField
.....
class Article(models.Model):
title = models.CharField(max_length=100)
content = TrixField('content')
time_added = models.DateTimeField(default=timezone.now)
def __str__(self):
return 'Article Name: {}, Added: {}'.format(self.title, self.time_added)
.....
它显示了类似的结果这张照片波纹管在我的Django的管理员,我爱:
class FeaturedArticle(ListView):
model = Article
context_object_name = 'featuredarticles'
template_name = 'blog/blog.html'
def get_queryset(self):
return Article.objects.filter(time_added__lte=timezone.now()).order_by('-time_added')[:5]
模板部分
<!-- article area -->
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<h2 class="blog-post-title">Featured Articles</h2>
<!-- tempalate tag here -->
{% for article in featuredarticles %}
<hr>
<h3>{{article.title}}</h3>
<p>{{article.content}}</p>
<!-- tempalate tag here -->
{% endfor %}
</div>
</div>
</div><br>
<!-- article area end -->
我该如何解决
article.content
包含 HTML,Django 将默认转义。您可以使用安全过滤器来确保内容不被转义:
{{ article.content|safe }}
这当然意味着可以将任意 HTML 插入到您的页面中,因此您应该小心谁可以编辑该字段。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句