问题
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"
I want use post_highlighted for put in a div only the article or the articles that have response true.
How I can set up the "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 %}
回答1:
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)
# ...
Now the object_list
will only contain PostModel
objects with post_highlighted = True
.
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)
so as long as object_list
contains the highlighted PostModel
objects, we are fine.
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
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.
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.
Note: typically models have no
Model
suffix. I advise you to rename the model toPost
.
回答2:
you can do this with simple if statement
{% for posts in object_list %}
{% if posts.post_highlighted %}
<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>
{% endif %}
{% endfor %}
来源:https://stackoverflow.com/questions/52121761/highlighted-post-in-django-templates