You should not filter in the template. This is not only inefficient, but a template is not meant to implement business logic, only rendering logic: a template should not be concerned with what it renders, it should only be concerned with rendering the data in a pleasant way.
You should filter in the ListView
:
class Home(ListView):
model = Post
template_name = 'home.html'
queryset = Post.objects.filter(featured=True)
This will filter at the database side.
If you need both the featured items, and the ones that are not featured, you can make two queries:
class Home(ListView):
model = Post
template_name = 'home.html'
queryset = Post.objects.filter(featured=True)
def non_featured(self):
return Post.objects.filter(featured=False)
then you can render the non-featured items with:
{% for post in view.non_featured %}
…
{% endfor %}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…