所以我试图在我的应用程序中创建一些审核。创建该帖子时,用户帖子应该为false,而主持人必须进入并设置为true才能生效。
我已将该字段添加到我的模型中。但是我正在努力让真正的模板显示在模板上。
型号:
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True, null=True)
image = models.ImageField(blank=True, null=True)
live = models.BooleanField(default=False)
观看次数:
class IndexView(ListView):
model = Post
template_name = "public/index.html"
我知道我需要使用if语句,但是我不确定如何实现它。谢谢。
答案 0 :(得分:1)
一种更好的方法是在视图中覆盖查询集,以仅获取实时帖子。
class IndexView(ListView):
queryset = Post.objects.filter(live=True)
template_name = "public/index.html"
现在您根本不需要更改模板。
答案 1 :(得分:0)
您必须在模板中使用if语句。
{% for object in object_list %}
{% if object.live %}
<div>
// Here you can put your Post
</div>
{% endif %}
{% endfor %}