Reputation: 13
I started on the generic views section of the Django tutorial on their website and i keep getting this error. I know this question has been asked before but these answer did not fix my issue. I was wondering could any see the error? I have removed imports to tidy the code.
urls.py:
app_name = 'polls'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(),
name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
views.py:
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
return Questions.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Questions
template_name = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = Questions
template_name = 'polls/results.html'
def vote(request, question_id):
question = get_object_or_404(Questions, pk=question_id)
try:
selected_choice = 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,)))
detail.html:
<h1>{{ object.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question_id=object.id %}" method="post">
{% csrf_token %}
{% for choice in object.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 %}
<input type="submit" value="Vote" />
</form>
results.html:
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{
choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:results' question.id %}">Vote again?</a>
Upvotes: 1
Views: 1020
Reputation: 308949
You have named your model Questions
instead of Question
like the tutorial has. The detail view uses the model name in lowercase for the model instance in the template. In your case this is questions
, which doesn't match the tutorial from the template which uses question
.
That means that you either need to set context_object_name
to 'question'
so that your template works:
class ResultsView(generic.DetailView):
model = Questions
context_object_name = 'question'
Or you need to change your template to use questions
instead of question
.
<h1>{{ questions.question_text }}</h1>
<ul>
{% for choice in questions.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{
choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:results' questions.id %}">Vote again?</a>
It would be a good idea to rename your model to Question
- the standard in Django is to use the singular instead of the plural for model names. If you do this, you'll have to run makemigrations
and then migrate
to rename the model. If that causes problems, it might be easier to delete your migrations and database, then run makemigrations
and migrate
on a fresh db.
Upvotes: 1