Oh, one note is that I am using postgresql.
everything else except vote function in views.py seems working.

Sorry, any help would be really appreciated!

Nori

On Saturday, February 2, 2019 at 10:02:14 PM UTC-5, Atsunori Kaneshige 
wrote:
>
> Hi Django users,
>
> I started using Django recently.
> I am following the official Django tutorial.
> I am just at Writing Your First Django App, Part4, and I have been just 
> copying and pasting all codes.
>
> But I have a problem in vote.
> I inserted print(question) and this is printed in detail.html
> also, question.id is also printed.
>
> BUT, choice part doesn't seem working.
>
> *<THIS IS views.py>*
> def vote(request, question_id):
>     question = get_object_or_404(Question, pk=question_id)
>     print(question)
>     try:
>         selected_choice = 
> question.choice_set.get(pk=request.POST['choice'])
>     except (KeyError, Choice.DoesNotExist) as e:
>         # Redisplay the question voting form.
>         print(e)
>         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,)))
>
> *<THIS IS details.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>
>
> <br>
> {{ question }}
> #printed 
>  <br>
> {{ question.id }}
> #printed
> <br>
> *{{ question.choice_set.all }}*
> *#<QuerySet []> #what is this? empty? why?*
> <br>
> {{ question.question_text }}
> #printed
> <br>
> <h1>{{ question.question_text }}</h1>
> #printed
> <ul>
> *{% for choice in question.choice_set.all %}*
> *    <li>{{ choice.choice_text }}</li>*
> *{% endfor %}*
> *#nothing printed!*
> </ul>
>
> Also when I click button 'vote', I only get error.
> *You didn't select a choice.*
>
> I am just copying and pasting so that I can understand Django, but I am 
> having hard time in this Part 4.
>
> I really appreciate advice from anybody!
>
> Nori
>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a581da0f-abd9-435e-8693-db9126b9bac1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to