Q&A
入力フォームが表示されないです。
Djnagoチュートリアルを見ながらWEBサイトを作っています。
https://docs.djangoproject.com/en/1.8/intro/tutorial04/
detail.htmlに
<!DOCTYPE html> <h1>{{ question.question_text }}</h1> {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} <form action="{% url 'poll_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> </html>
とコードを書きました。これにより本来
と表示されるはずなのですが
と今表示されていてラジオボタンが表示されません。
views.pyには
from django.shortcuts import render from django.utils.html import mark_safe from .models import Question from django.http import HttpResponse from django.shortcuts import Http404 from django.shortcuts import get_object_or_404,redirect from .models import Choice # Create your views here. def index(request): # return HttpResponse("Hello,world.You're at the polls index.") return render(request,'polls/index.html',{ # 'hoge':'test string', # 'fuga':'<br>tag</br>', # 'piyo': mark_safe('<br>tag</br>'), 'questions': Question.objects.all(), }) def detail(request,pk): try: obj = Question.objects.get(pk=pk) except Question.DoesNotExist: raise Http404 return render(request,'polls/detail.html',{ 'question':obj, }) def vote(request,pk): question = get_object_or_404(Question,pk=pk) try: selected_choice = question.choice_set.get(pk=request.POST['choice']) except (KeyError,Choice.DoesNotExist): return render(request,'poll/detail.html',{ 'question':question, 'error_message':"You didn't select a choice", }) else: selected_choice.votes += 1 selected_choice.save() return redirect('index') def results(request,pk): obj = get_object_or_404(Question,pk=pk) return render(request,'polls/results.html',{ 'question':obj, })
と書きました。なぜラジオボタンが表示されないのでしょうか?
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。