Tutorial 3 template logic puzzle

I’m having an odd result when the polls index.html is rendered (http://localhost:8000/polls/). It renders the else in the if loop only. The initial code block returns False or None and the for loop is never executed. Utilizing the tutorials earlier HttpResponse code block resulted in the same issue. (Does anyone know of a good debugger the template files?)

I also used manage.py check with the relevant imports and got a return value on the variable in play.

ans = lastest_question_list = Question.objects.order_by('-pub_date')[:5]

Where ans returned the question input from tutorial 2.

(This is not similar to: Tutorial building index template). That was the only result for ‘template’ in the past 6 months.

Rendered result:

Spaces removed for brevity. Code does conform to PEP 8 (Pycharm Pro). Inspecting the code only results with the index.html file path not existing.


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Current Poll Listings</title>
{% if latest_question_list %}
    {% for question in latest_question_list %}
    <a href="/polls/{{ question.id }}/">{{ question.question_text }}</a>
    {% endfor %}
{% else %}
    <br><p>No polls are available.</p>
{% endif %}

The relevant code in mysite/mysite/pollsviews.py is:

from django.http import HttpResponse
from django.shortcuts import render
from .models import Question

def index(request):
    lastest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'lastest_question_list': lastest_question_list}
    return render(request, 'polls/index.html', context)

mysite/mysite/polls/models.py, Question class only.

from django.db import models
from django.utils import timezone
import datetime

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

I can attach the complete source files is this isn’t enough to go on, or any other relevant code from the exercise. Despite looking over the code until my eyes burned and using Pycharms code inspection option nothing stands out. Then again I’m a guy who was writing some C code that would not compile at all. Spent three days looking at the code only to realize the Main(void) line at the top was the entire issue.

Note: I did not create the project as a Django Project in Pycharm but as just a Python Project so I could do it barebones so to speak. The IDE can help me later once I learn, but I don’t need it doing the work for me or I won’t learn a thing.

Notice what you’re checking in your if.
Now, compare that carefully to what you’re setting in your context:


1 Like

Bang, Bang! Bang! (sound of head striking table with face red as beet)


Thank you for the fix and lesson I’d had long ago … but to make it in front of everybody … nothing like a public shaming to make me more aware of typos like this and learn from it.

You have a very sharp eye and caught this pretty darn quickly.

Thank you again for the quick reply with problem area exposed. I had suspected the issue was with the HTML code but missed tree for the forest.


No shame, no worries, we’ve all been there at one time or another. Over the years I’ve had it drilled into me many times to “look for horses, not zebras”.