我创建了这个模型:
class PostModel(models.Model):
post_title = models.CharField(max_length=70)
post_short_description = models.TextField(max_length=200)
post_contents = models.TextField()
post_publishing_date = models.DateTimeField(auto_now=False, auto_now_add=True)
post_author = models.ForeignKey(AuthorModel, on_delete=models.CASCADE)
post_keyconcept = models.ManyToManyField(KeyConceptModel)
slug = models.SlugField(verbose_name="Slug", unique="True")
post_highlighted = models.BooleanField(default=False)
def __str__(self):
return self.post_title
def get_absolute_url(self):
return reverse("singlepostManuscriptusView", kwargs={"slug": self.slug})
class Meta:
verbose_name = "Articolo"
verbose_name_plural = "Articoli"
我想使用post_highlighted仅将文章或响应为true的文章放入div中。
如何设置“for cicle”?
这里有用于显示帖子列表的 cicle:
{% for posts in object_list %}
<div id="bloghome" class="container">
<h1><a href="{{ posts.get_absolute_url }}">{{ posts.post_title }}</a></h1>
<p>{{ posts.post_short_description|safe|linebreaks }}</p>
<p>Pubblicato il <strong>{{ posts.post_publishing_date|date }}</strong></p>
<h5>Keywords:</h5>
{% for keyword in object_list.all %}
<button type="button" class="btn btn-outline-warning btn-sm">{{ keyword }}</button>
{% endfor %}
</div>
<hr>
{% empty %}
<h1>Go to the admin panel and create your first post!</h1>
{% endfor %}