因此,我正在尝试在我的应用程序中创建一些适度。当创建帖子和版主必须进来并将其设置为true时,用户帖子应该是false才能上线。
我已经将该字段添加到我的模型中。但我正在努力让真实的数据显示在我的模板上。
型号:
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语句,但我不确定如何实现它。谢谢。
发布于 2019-03-12 05:06:23
一种更好的方法是覆盖视图中的查询集,以便只获取实时帖子。
class IndexView(ListView):
queryset = Post.objects.filter(live=True)
template_name = "public/index.html"现在,您根本不需要更改模板。
发布于 2019-03-12 05:02:26
您必须在模板中使用if语句。
{% for object in object_list %}
{% if object.live %}
<div>
// Here you can put your Post
</div>
{% endif %}
{% endfor %}https://stackoverflow.com/questions/55110305
复制相似问题