Djangoのチュートリアルをやっています。
現在その4まで来ましたが、ラジオボタンとそのラベルが表示できません
おそらくfor文が表示されていないのだと思います。
最後の"Vote"ボタンは表示できます。
追記
choise_textのスペルが間違えていたので、修正したくDBを少し触りました。
Python 3.7.0 (default,--略-- >>> import django >>> print(django.get_version()) 2.1.7
mysite>polls>templates>polls>detail.html
<h1>{{ question.question_text }}</h1> {% if error_message %}<p><strong>{{ error_message }}</p></strong>{% 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>
$ cat views.py from django.shortcuts import render from django.http import HttpResponse from .models import Choice, Question from django.shortcuts import get_object_or_404 def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list':latest_question_list} return render(request, 'polls/index.html', context) def detail(request,question_id): question = get_object_or_404(Question, pk=question_id) 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): question = get_object_or_404(Question, pk=question_id) try: selected_choice = question.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render(request, 'polls/detail.html', { 'question':question, 'error_message':"You didn't select a choice.", }) else: selected_choice.votes += 1 selected_choice.save() return HttpResponseRedurect(reverse('polls:results', args=(question.id,)))
WEBの開発者ツールから確認したHTML(detal.html)
<html><head></head><body><h1>What's New?</h1> <form action="/polls/1/vote/" method="post"> <input type="hidden" name="csrfmiddlewaretoken" value="yJ7WrTgwfPuoWWvXHuso7lkX0UdJjelyx25vH2kmAqieBb74n4MqT1G0JcR53qKK"> <input type="submit" value="Vote"> </form> </body></html>
$ python manage.py sqlmigrate polls 0001 BEGIN; -- -- Create model Choice -- CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choise_text" varchar(288) NOT NULL, "votes" integer NOT NULL); -- -- Create model Question -- CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(288) NOT NULL, "pub_date" datetime NOT NULL); -- -- Add field question to choice -- ALTER TABLE "polls_choice" RENAME TO "polls_choice__old"; CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choise_text" varchar(288) NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "polls_question" ("id") DEFERRABLE INITIALLY DEFERRED); INSERT INTO "polls_choice" ("id", "choise_text", "votes", "question_id") SELECT "id", "choise_text", "votes", NULL FROM "polls_choice__old"; DROP TABLE "polls_choice__old"; CREATE INDEX "polls_choice_question_id_c5b4b260" ON "polls_choice" ("question_id"); COMMIT;
無理やりパス指定してみれば、ラジオボタンで表示させたいものがDB内にあることは確認できました
/not muchとthe skyを表示させたい(forでまわしたい)
<html><head></head><body><h1>What's New?</h1> <ul> <li>Not much -- 0 votes</li> <li>The sky -- 1 vote</li> </ul> <a herf="/polls/1/">Vote again?</a> <div></div></body></html>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。