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.
mysite/mysite/polls/templates/polls/Index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Current Poll Listings</title>
</head>
<body>
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><br><p>
<a href="/polls/{{ question.id }}/">{{ question.question_text }}</a>
</p></li>
{% endfor %}
</ul>
{% else %}
<br><p>No polls are available.</p>
{% endif %}
</body>
</html>
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.