Forums

QuerySet not appearing in HTML file (DjangoGirls tutorial)

I am undertaking the "Django Templates" section of the django girls tutorial and I cannot get my local server to display posts from the database.

1) The QuerySet isn't loading at all in the HTML file.

I followed the steps to import the Post model into views.py and add the QuerySet in the posts variable (the previous part of the tutorial). When I tried putting {{ posts }} in the HTML file (post_list.html), nothing appears in the QuerySet that loads on the page.

This is all on my local machine for now but I want it to work here before I push to pythonanywhere. When I query the database on my venv it returns the data I was expecting (dates, titles, etc.) however when I try and pull from database all I get is the web title appear on the page. Not sure what is going wrong, everyone seems to have issues forgetting that the database isn't committing but I haven't reached that point yet! :)

I have included the code so far (it matches the tutorial as I copy pasted to error check) and I am stumped. Like I say no data is displayed (posts) and only the title "Web App" appears at the top of the page. Help! :D

post_list.html

<html>
<head>
    <title>Web app</title>
</head>
<body>
    <div>
        <h1><a href="/">Web app</a></h1>
    </div>

    {% for post in posts %}
        <div>
            <p>published: {{ post.published_date }}</p>
            <h1><a href="/">{{ post.title }}</a></h1>
            <p>{{ post.text|linebreaksbr }}</p>
        </div>
    {% endfor %}

</body>

</html>

view.py

from django.shortcuts import render
from django.utils import timezone
from .models import Post

def post_list(request):
posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, 'blog/post_list.html', {'posts': posts})

The error was not setting publishing dates to the blog post

OK -- is it all working now, then?

Yeh, all working. Didn't notice it was using published date rather than creation date :)

Glad you worked it out :-)