In the picture above, I am trying to find a way to have 6 featured posts from my database appear instead of just six static entities in HTML.
Here is the view for the home page:
from django.shortcuts import render
from singlePost.models import Post
# Create your views here.
def index(request):
queryset = Post.objects.filter(featured=True)
context = {
'object_list': queryset
}
return render(request, 'index.html', context)
Here is a little bit of code from the home page HTML (looping through object_list):
{% for obj in object_list %}
{% if forloop.counter0 == 0 %}
<h3 class="mb-15 mb-sm-5 font-sm-13"><b>{{ obj.title }}</b></h3>
{% endif %}
{% endfor %}
My question is: how can I get the indices of object_list so I can just use the first 6 featured Posts?
I do not know how to do that, so it is currently looping through all posts and I use an if as seen above to check the current index, but that just seems wrong looping 6 times instead of using indices. The loop would be fine surrounding a div if all the divs were the same, but as you see in the picture, they are not.
So, how do I get the indices of a QuerySet? Or are there any better ways to do this then the two ways that I am thinking of?
Thank you