Dom Shahbazi
Dom Shahbazi

Reputation: 730

Why is my django template not outputting template variable object?

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

Upvotes: 1

Views: 175

Answers (1)

Alasdair
Alasdair

Reputation: 309129

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

Upvotes: 2

Related Questions