【发布时间】:2020-05-07 11:38:52
【问题描述】:
models.py:
class post(models.Model):
title = models.CharField(max_length=40)
picture = models.ImageField(upload_to='somewhere/dir')
active_post = models.BooleanField(default=True)
commercial_post = models.BooleanField(default=False)
normal_post = models.BooleanField(default=True)
post_with_colored_border = models.BooleanField(default=False)
post_title_bold = models.BooleanField(default=False)
post_with_border_and_title_bold = models.BooleanField(default=False)
def __str__ (self):
return self.title
views.py:
def index(request):
posts= post.objects.filter(active_post=True)
normal_post = posts.filter(normal_post=True)
commercial_post = posts.filter(commercial_post=True)
post_with_border = posts.filter(post_with_colored_border=True)
post_title_bold = posts.filter(post_title_bold=True)
post_with_border_and_title_bold = posts.filter(post_with_border_and_title_bold=True)
context = {
'posts':posts,
'commercial_post':commercial_post,
'post_with_border':post_with_border,
'post_title_bold':post_title_bold,
'post_with_border_and_title_bold':post_with_border_and_title_bold,
}
return render(request, 'index.html', context)
index.html:
{% if post_with_border AND commercial_post %}
{% for abc in posts %}
<div>
<a href="#" class="border border-danger">
<img src="{{abc.picture.url}}">
</a>
<h1>
<a href="#" class="SomeClass"> {{abc.title}}
</a>
</h1>
</div>
{% endfor %}
{% else %}
{% for abc in normal_post %}
<div>
<a href="#">
<img src="{{abc.picture.url}}">
</a>
<h6>
<a href="#"> {{abc.title}}
</a>
</h6>
</div>
{% endfor %}
{% endif %}
我想让它列出如果它是商业帖子,它应该有一个边框和标题中的 H1,但如果它是一个普通的帖子,它应该没有边框和 H6。但问题是它显示所有带有边框和 H1 的帖子。 我需要你的帮助
谢谢
【问题讨论】:
标签: django for-loop if-statement conditional-statements