まず
python
1- view.py 2 3 4class DetailView(generic.DetailView): 5 model = Question 6 template_name = 'polls/detail.html' 7 def __init__get_queryset(self): 8 return Question.objects.filter(pub_date__lte=timezone.now()) 9 10class CommentView(DetailView): 11 pass 12 13
python
1- urls.py 2 3path('<int:pk>/', views.CommentView.as_view(), name='detail'),
classを継承して投票フォームが表示されました。
ちなみにhtml部分はこのようになっており、ここまでは問題が発生しておりません
html
1 2- detail.html 3 4<h1>{{ question.question_text }}</h1> 5 6{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} 7 8<form action="{% url 'polls:vote' question.id %}" method="post"> 9{% csrf_token %} 10{% for choice in question.choice_set.all %} 11 <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"> 12<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br> 13{% endfor %} 14 15 16<input type="submit" value="投票"> 17 18</form> 19 20{{ message}} 21
ここから- view.pyのCommentViewを改造し、
python
1 2- view.py 3 4class CommentView(DetailView): 5 def __init__index(request): 6 return HttpResponse("Hello, World") 7
とすれば投票フォームの下にHello, Worldと表示されると思ったのですが何も表示されません。
どのようにコードを書き換えればHello, Worldが表示されるでしょうか?
本当はもっと複雑なもの(コメントフォーム)を表示させるつもりなのですが、まずは簡単なものを表示させようと思い実験しているのですがうまくいきません。
もしよければアドバイスをお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。