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

On Tue, Mar 14, 2023, 20:34 Nithin Kumar <code.nithi...@gmail.com> wrote:

> 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)
>
>
>
>
>
> On Tuesday, March 14, 2023 at 1:22:25 PM UTC-4 Prosper Lekia wrote:
>
>> Let's see your views.
>>
>> On Tue, Mar 14, 2023, 14:32 Muhammad Juwaini Abdul Rahman <
>> juw...@gmail.com> wrote:
>>
>>> question_id=question.id
>>>
>>> On Tue, 14 Mar 2023 at 21:22, Nithin Kumar <code.n...@gmail.com> wrote:
>>>
>>>> Hi,
>>>>
>>>> Stuck with this problem
>>>>
>>>> https://docs.djangoproject.com/en/4.1/intro/tutorial04/
>>>>
>>>> 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>
>>>>
>>>> --
>>>> 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
>>>> <https://groups.google.com/d/msgid/django-users/d6c40407-64a0-4418-ba9a-39db89b1c1dcn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> --
>>> 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/CAFKhtoQ1tfqUempakxY7DHy26utEt3QN1iBbLBM78r5neYM3QQ%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/django-users/CAFKhtoQ1tfqUempakxY7DHy26utEt3QN1iBbLBM78r5neYM3QQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/1d230ed2-73f0-4609-b53b-e620dff5d302n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/1d230ed2-73f0-4609-b53b-e620dff5d302n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALGeGE1E%3DCTtpEQpvxDJrMzW9qrzdwfFMUd54b8CJ_bk8URbRw%40mail.gmail.com.

Reply via email to