Stuck with Django Tutorial Part 4

104 views
Skip to first unread message

Nithin Kumar

unread,
Mar 14, 2023, 9:23:07 AM3/14/23
to Django users
Hi,

Stuck with this problem 


NoReverseMatch at /polls/2/Reverse for 'vote' with arguments '(2,)' not found. 1 pattern(s) tried: ['polls/<int:question_id/vote/\\Z']

My detail.html is like this and it is failing at Line 1. 
I checked all solutions online but no luck. 

<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>

Muhammad Juwaini Abdul Rahman

unread,
Mar 14, 2023, 9:32:32 AM3/14/23
to django...@googlegroups.com
question_id=question.id

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d6c40407-64a0-4418-ba9a-39db89b1c1dcn%40googlegroups.com.

Prosper Lekia

unread,
Mar 14, 2023, 1:22:25 PM3/14/23
to django...@googlegroups.com

Nithin Kumar

unread,
Mar 14, 2023, 3:33:13 PM3/14/23
to Django users
question.id or question_id both gave the same result.
These are the views.

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.template import loader
from .models import Choice,Question
from django.urls import reverse

# Create your views here.

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

def detail(request, question_id):
    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question Does not exist")
    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):
    return HttpResponse("You're voting on question %s." % question_id)
   



Sandip Bhattacharya

unread,
Mar 14, 2023, 3:44:56 PM3/14/23
to django...@googlegroups.com
Can you share your urls.py?


Brian Carey

unread,
Mar 14, 2023, 5:08:12 PM3/14/23
to django...@googlegroups.com
I think you need to check your urls.py.

Prosper Lekia

unread,
Mar 14, 2023, 5:28:35 PM3/14/23
to django...@googlegroups.com
This should be your views for vote.


from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse

from .models import Choice, Question
# ...
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()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

Nithin Kumar

unread,
Mar 14, 2023, 7:31:37 PM3/14/23
to django...@googlegroups.com
Here.

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'),
]


You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/8mapBVGvyMA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/A42F210A-ED92-49F6-A647-D57C4C6814B0%40showmethesource.org.

Chetan Ganji

unread,
Mar 15, 2023, 12:13:37 PM3/15/23
to django...@googlegroups.com
It seems to me like you are not entering the url name
In place of 'polls:vote' try to enter the name of the url given to that url For Your Reference https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#url
I hope it helps you!
Regards,
Chetan Ganji
+91-900-483-4183


Reply all
Reply to author
Forward
0 new messages