i’m having this problem: Suppose you’ve a blog application and you want to display all the posts created. But you have Posts that can be “sticky” or “featured” that have to be displayed first and must reside in a different html “block”. Maybe it’s not the best example, but after all is what i need.
So, the model is simple:
class Post(models.Model):
title = models.CharField()
content = models.TextField()
featured = models.BooleanField(default=False)
created = models.DateTimeField(auto_now=False, auto_now_add=True)
class Meta:
ordering = ['-featured','-created']
In the view i’m just querying all the posts and displaying it in a template:
def my_view(request):
return render_to_response('template.html',{'posts':Post.objects.all()})
Now, the problem is in my template, what i want to have as a result is this:
<html>
<div class='featured-posts'>
<ul>
<li> A Featured post</li>
</ul>
</div>
<div class='not-featured-posts'>
<ul>
<li> A NON Featured post</li>
</ul>
</div>
</html>
What can i do? I was thinking that maybe I could grab those separated, this way:
return render_to_response('template.html',{
'featured':Post.objects.filter(featured=True),
'non_featured':Post.objects.filter(featured=False)
})
But I really don’t like that approach, is there any “template-based” solution?
Thx!
Regroup on the
featuredfield.