The poblem with Documentation,Writing your first Django app

TemplateDoesNotExist at /polls/
polls/question_list.html
Django tried loading these templates, in this order:

Using engine django:

  • django.template.loaders.filesystem.Loader: D:\Work\mysite\templates\polls\question_list.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: D:\Work\mysite\polls\templates\polls\question_list.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: D:\Work\env\lib\site-packages\django\contrib\admin\templates\polls\question_list.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: D:\Work\env\lib\site-packages\django\contrib\auth\templates\polls\question_list.html (Source does not exist)

How can I fix it?

First you need to show what code you have in urls.py and views.py, I’m taking it that you have already set up the html template in the “templates” folder for the missing html template called “question_list.html”

urls.py

from django.urls import path
from . import views

app_name = ‘polls’
urlpatterns = [
path(‘’, views.IndexView.as_view(), name=‘index’),
path(‘int:pk/’, views.DetailView.as_view(), name=‘detail’),
path(‘int:pk/results’, views.DetailView.as_view(), name=‘results’ ),
path(‘int:question_id/vote/’, views.vote, name=‘vote’),
]

views.py

from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic
from .models import Choice, Question
from django.utils import timezone

Create your views here.

class IndexView(generic.ListView):
templates_name = ‘polls/index.html’
context_object_name = ‘latest_question_list’

def get_queryset(self):
    """return the last five published questions."""
    return Question.objects.filter(
        pub_date__lte=timezone.now()
    ).order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
model = Question
templates_name = ‘polls/detail.html’

class ResultsView(generic.DetailView):
model = Question
templates_name = ‘polls/results.html’

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

def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, ‘polls/detail.html’, {‘question’: question})

def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, ‘polls/results.html’, {‘question’: question})

def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.question.choice_set.get(pk=request.POST[‘choice’])
except (KeyError, Choice.DoesNotExist):
return render(request, ‘polls/detail.html’, {
‘question’: question,
‘error_message’: “You didn’t select a choice”,
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse(‘polls:results’, args=(question.id,)))

def get_queryset():
return Question.objects.filter(pub_date__lte=timezone.now())

First, a side note.

When posting code here, exclose the block of code of code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted, which is critical for Python code.

Now, your error is caused by a typo in your IndexView. Compare your code carefully with the code in the tutorial.

1 Like

However, new errors have emerged

NoReverseMatch at /polls/

Reverse for ‘vote’ with arguments ‘(’‘,)’ not found. 1 pattern(s) tried: [‘polls/(?P<question_id>[0-9]+)/vote/\Z’]
In template D:\Work\mysite\polls\templates\polls\index.html , error at line 13

index.html

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
{% if latest_question_list %}
   <ul>
   {% for question in latest_question_list %}
       <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
   {% endfor %}
   </ul>
{% else %}
   <p>No polls are available.</p>
{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
<fieldset>
   <legend><h1>{{ question.question_text }}</h1></legend>
   {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
   {% for choice in question.choice_set.all %}
       <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
       <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
   {% endfor %}
</fieldset>
<input type="submit" value="Vote">
</form>

It looks to me like you’ve combined two different templates from the tutorials. What you’re showing here as your index.html has more than just what should be in index.html.

Review again the tutorial steps regarding these templates.

1 Like