Django模板中的突出显示的帖子 [英] Highlighted post in Django templates
问题描述
我已经创建了这个模型:
I've created this model:
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 来放入 div 的文章或具有响应 true 的文章.
I want use post_highlighted for put in a div only the article or the articles that have response true.
如何设置"for cicle"?
此处显示要发布的帖子列表:
Here there is the cicle for show the list of posts:
{% 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 %}
推荐答案
不要在模板中执行此操作,您可以更改ListView
的queryset
(基于object_list
,我假设您使用ListView
或至少一个相关类):
Don't do this in the template, you can change the queryset
of the ListView
(well based on the object_list
I assume you use a ListView
or at least a related class):
class MyPostView(ListView):
model = PostModel
queryset = PostModel.objects.filter(post_highlighted=True)
# ...
现在object_list
将只包含带有post_highlighted = True
的PostModel
个对象.
Now the object_list
will only contain PostModel
objects with post_highlighted = True
.
如果您使用另一个视图来自己构造object_list
,则可以这样写:
If you use another view where you construct the object_list
yourself, you can write it like:
object_list = PostModel.objects.filter(post_highlighted=True)
只要object_list
包含突出显示的PostModel
对象,就可以了.
so as long as object_list
contains the highlighted PostModel
objects, we are fine.
在queryset
级别执行此操作的优点是,我们将查询数据库以查找突出显示的PostModel
对象.因此,我们执行查询:
The advantage of doing this at the queryset
level is that we will query the database for highlighted PostModel
objects. We thus perform a query:
SELECT postmodel.*
FROM postmodel
WHERE post_highlighted = TRUE
数据库通常可以有效地搜索此类记录,此外,我们避免在Django/Python级别进行过滤,这通常会慢很多.
Databases typically can efficiently search for such records, and furthermore we avoid doing the filtering at Django/Python-level, which is usually a magnitude slower.
想象一下,如果突出显示了一百个帖子中的两个.然后,通过不在数据库级别进行过滤,您可以将所有一百个帖子的数据传输到Django,进行反序列化,遍历这些帖子,并几乎立即丢弃掉未突出显示的98个帖子.这是一项繁重的工作.
Imagine if two posts out of hundred are highlighted. Then by not filtering at the database level, you transfer the data of all hundred posts to Django, do the deserialization, loop over these, and throw away the 98 non-highlighted almost instantly. This is a lot of work for nothing.
注意:通常型号没有后缀
Model
.我建议您将模型重命名为Post
.
Note: typically models have no
Model
suffix. I advise you to rename the model toPost
.
这篇关于Django模板中的突出显示的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!