help with Django tutorial "writting our first Django app" part 4

35 views
Skip to first unread message

Rafael Valle Cabrera

unread,
Jul 13, 2017, 8:41:52 AM7/13/17
to django...@googlegroups.com

 

Hi everyone

 

I am getting started with Django.

 

My doubt is about the tutorial called ‘Writing our first Django app’

 

For everyone who doesn’t know about it, it’s a tutorial where you start developing a polls app.

I am in the part where i have to do the forms.

 

I am doing part 4:

https://docs.djangoproject.com/en/1.11/intro/tutorial04/

and it’s happening to me the following situation:

 

 

polls/templates/polls/detail.html

 

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

 

 

polls/views.py

 

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
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,)))

 

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

 

 

 

polls/templates/polls/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:detail' question.id %}">Vote again?</a>

 

 

I am doing the polls app, and when i hit the button ‘Vote’, the page always shows me the error message:

“You didn’t select any choice.”

 

Moreover, it doesn’t show any radio button to select a certain choice and once i have hit the button, it never redirects me to the results page. Also, i can’t see the number of votes in the results page.

 

I have reviewed several times the code, and it looks like the same as the tutorial one. (Even, i have replaced it but it doesn’t work)

 

Any idea about why the input element doesn’t show in the choice page and doesn’t allow me to vote?

 

Thanks for help beforehand

Rafa

 

 

https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif

Libre de virus. www.avast.com

 


Libre de virus. www.avast.com
Reply all
Reply to author
Forward
0 new messages