0

Home Page

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

4 Answers 4

2

In index() you have define queryset as follows

queryset = Post.objects.filter(featured=True).order_by("-id")[:6]

By order_by("-id") will return latest featured post first and [:6] this will give only 6 post.

https://docs.djangoproject.com/en/2.2/ref/models/querysets/#reverse

Sign up to request clarification or add additional context in comments.

1 Comment

How would you use that in HTML to get the 2nd index? I tried {{ object_list.get(id=2) }} and {{ object_list[2] }} but it did not work.
0

I finally figured it out. The other answers are very helpful, but not exactly what I was looking for.

If you simply want to use the index of a QuerySet:

object_list.2

where this gives you the 2nd index

Comments

0

I got your problem so here is another solution for getting index of object in queryset. So need to get index of objects in view because in html querying doesn't work.

Take another list of dict, {"index": index+1, "object": obj} append in list that can call in html page. and as you want only title of post you can set {"index": index+1, "object": obj.title} which can reduce database hits.

Here is the view as follow:

from django.shortcuts import render
from singlePost.models import Post

# Create your views here.
def index(request):
    queryset = Post.objects.filter(featured=True).order_by("-id")[:6]
    indexed_queryset = []
    for index, obj in enumerate(queryset):
        indexed_queryset.append({"index": index+1, "object": obj})
    context = {
        'object_list': indexed_queryset
    }
    return render(request, 'index.html', context)

And HTML page would be as follows:

{% for obj in object_list %}
{% if obj.index == 0 %}
<h3 class="mb-15 mb-sm-5 font-sm-13"><b>{{ obj.object.title }}</b></h3>
{% endif %}
{% endfor %}

you can change the condition if obj.index == 0 which index you want. hope this will solve your problem, let me know.

Comments

0

In index() you have define queryset as follows

queryset = Post.objects.filter(featured=True).order_by("-id")[:6]

By order_by("-id") will return latest featured post first and [:6] this will give only 6 post.

https://docs.djangoproject.com/en/2.2/ref/models/querysets/#reverse

for template watch below may work

{{ some_list|slice:":2" }}

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slice

3 Comments

How would you use that in HTML to get the 2nd index? I tried {{ object_list.get(id=2) }} and {{ object_list[2] }} but it did not work.
{% if forloop.counter == 2 %} this will count the 2 index only
Is there a way to get the 2nd index without the for loop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.