Kronos
Kronos

Reputation: 53

NoReverseMatch at - django polls app tutorial

I keep getting this when trying to load da page:

NoReverseMatch at /polls/
Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']

In template C:\Users\sarah\Desktop\django2\myproject\my_site\polls\templates\polls\index.html, error at line 20

line 20:

<form action="{% url 'polls:vote' question.id %}" method="post">

I am a complete beginner at django, css, html, ... I kept checking in with the tutorial, comparing my code with the code reference in the tutorial, however I see no mistake.

My 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 %}

<h1>{{ question.question_text }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% 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 %}
<input type="submit" value="Vote">
</form>

The vote function in views.py:

def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting form.
        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,)))

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.ResultsView.as_view(), name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),

Thank you.

Upvotes: 1

Views: 84

Answers (1)

Harben
Harben

Reputation: 1856

The main reason here is you are using your index.html with the form where you should really have this in polls/detail where you have access to a question object by pk. Right now you are trying to access the id property of question but question is undefined in your index.html.

Place the following in your polls/details.html

<h1>{{ question.question_text }}</h1>
<form action="{% url 'polls:vote' question.id %}" method="post">
    {% csrf_token %}
    {% 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 %}
    <input type="submit" value="Vote">
</form>

Then, navigate to polls/1 or whatever question.id you want and then try and use the form.

Upvotes: 1

Related Questions