1

I am having issues getting my template to output my Postobjects, which represent a blog post. On my main index page my blog posts display just fine, and i have no problem loading the template variables. When a user clicks one of the archive dates, which represent an archive link to display posts from a certain day and month, the same index page is used but given a different set of posts to display, filtered by date.

views.py

def month(request, year, month):
    """Monthly archive."""
    posts = Post.objects.filter(created__year=year, created__month=month)

    d = dict(posts=posts, user=request.user, months=mkmonth_lst(), archive=True)
    return render(request, 'blog/index.html', d)

index.html

{% for post in posts.object_list %}
        <div class="blogpost">
            <div class="blogpost-title"><h2>{{ post.title }}<h2></div>
            <div class="blogpost-meta"><h4>{{ post.created }}</h></div>
            <div class="blogpost-body">{{ post.body|linebreaks }}</div>
            <div class="blogpost-comments"><a href="{% url 'post' post.id %}">Comments</a></div>
        </div>          
        {% endfor %}

In the above month function, after retrieving the list of Post objects and counting them i can see they are being retrieved. The problem is the index page not outputting them.

Can anyone help? Thanks

1 Answer 1

2

In your template you should loop through posts, not posts.object_list.

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

4 Comments

Thankyou! that is weird, i was originally using posts.object_list to output my entire collection of posts, yet it didnt work when outputting the subset of posts.
Were you using pagination in the other view, or using a class based view that does pagination?
Yes the other view uses pagination, but shares the template with months. The pagination still seems to work using just posts, is there a reason i read somewhere to use posts.object_list?
The example in the docs doesn't loop through object_list, so I think it's fine to loop through posts if it works for you.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.