前提・実現したいこと
Djangoを理解したくてDjangoの公式ホームページにあるDocumentationにある
tutorialsをやってます。全てコピー&ペーストでやっていて、読みながらじっくりやってるのですが、Part4のコードがどうしても動きません。以下が、トラブっているコードです。
もし、Djangoのtutorialをやった方、いらっしゃいましたが、アドバイスいただけると大変嬉しいです。
発生している問題・エラーメッセージ
<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!
I really want to become a good Django user!
エラーメッセージ
該当のソースコード
ソースコード
試したこと
questionをprintしてみたり、question.idをプリントしてみたりしまして、
それは問題なくprintされます。しかし、choiceの部分が何かがおかしいと思います。
question.choice_set.allをprintしてみても、<QuerySet []>とprintされて、空っぽの状態です。
もし、Djangoの公式サイトのtutorialをやられた方いらっしゃいましたら、何か教えていただけると大変嬉しいです。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー