前提・実現したいこと
python=3.7.0
django=(2, 0, 2, 'final', 0)
の環境で開発しています。
Djangoチュートリアルで学んだ投票アプリに改造を加えコメント投稿機能を追加しております。
Djangoチュートリアル。
https://docs.djangoproject.com/ja/2.1/intro/tutorial01/
投票ページ。ラジオボタンで投票できます。投票ボタンを押すとページが遷移
投票結果が表示されコメントをどうぞのリンクでコメントページに遷移
コメントの一覧ページに飛ぶ(ただしコメントが誰に投票したのかと紐づいていない)
というようになっているのですが、ここで選択した選択した名前を
「投票ありがとうございました。**<選択した名前>**にコメントをどうぞ」
というように選択した名前を引き継ぎたいのです。
それができればフォームページ、コメントページにも名前を引き継いでいき、コメント一覧ページで誰に対してそのコメントが投稿されたかわかるようになると思ったからです。
コードとしましては
views.py
python
1class DetailView(generic.DetailView): 2 model = Question 3 template_name = 'polls/detail.html' 4 def get_queryset(self): 5 return Question.objects.filter(pub_date__lte=timezone.now())
models.py
python
1class Choice(models.Model): 2 question = models.ForeignKey(Question, on_delete=models.CASCADE) 3 choice_text = models.CharField(max_length=200) 4 votes = models.IntegerField(default=0)
detail.html
html
1<h1>{{ question.question_text }}</h1> 2 3{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} 4 5<form action="{% url 'polls:vote' question.id %}" method="post"> 6{% csrf_token %} 7{% for choice in question.choice_set.all %} 8 <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"> 9 <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br> 10{% endfor %} 11<input type="submit" value="投票"> 12</form> 13 14
ここでviews.pyの関数def voteで投票数を増加させpolls:resultsにリダイレクトしています。
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': "投票内容を選んでください", }) else: selected_choice.votes += 1 selected_choice.save() return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
resultページはこのような構成で表示されています。
views.py
python
1class ResultsView(generic.DetailView): 2 model = Question 3 template_name = 'polls/results.html' 4
models.py
python
1class Question(models.Model): 2 question_text = models.CharField(max_length=200) 3 pub_date = models.DateTimeField('date published') 4
results.html
html
1<h1>{{ question.question_text }}現在までの集計</h1> 2 3<ul> 4{% for choice in question.choice_set.all %} 5 <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li> 6{% endfor %} 7</ul> 8 9<a>投票ありがとうございました</a> 10<a href="{% url 'polls:comment' question.id %}">コメントをどうぞ</a>
urls.py
python
1from django.urls import path 2 3from . import views 4 5app_name = 'polls' 6urlpatterns = [ 7 path('', views.IndexView.as_view(), name='index'), 8 path('<int:pk>/', views.DetailView.as_view(), name='detail'), 9 path('<int:pk>/results/', views.ResultsView.as_view(), name='results'), 10 path('<int:question_id>/vote/', views.vote, name='vote'), 11 path('<int:post_pk>/comment/', views.CommentView.as_view(), name='comment'), 12 path('<int:pk>/comment_list/', views.commentlist, name='comment_list'), 13] 14
となっておりこれを改造しようとしています。
試したこと
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': "投票内容を選んでください", }) else: selected_choice.votes += 1 selected_choice.save() context ={ 'comment_ichiran':selected_choice, ←追加部分 } return HttpResponseRedirect(reverse('polls:results', args=(question.id,)), context←追加部分)
html
1<h1>{{ question.question_text }}現在までの集計</h1> 2 3<ul> 4{% for choice in question.choice_set.all %} 5 <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li> 6{% endfor %} 7</ul> 8 9<a>投票ありがとうございました</a> 10 11{% for list in comment_ichiran %} 12{{ list.choice_text }} ←追加部分 13{% endfor %} 14 15<a href="{% url 'polls:comment' question.id %}">コメントをどうぞ</a>
これでうまい具合に選択したテキストだけが表示されればよいなと思いましたがなにも表示されませんでした。
python
1question.choice_set.get(pk=request.POST['choice'])
で選択したpkをうまく次のページに引き継げばよいと思っているのですが現状ではうまくいっていません。
アドバイスがあればよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。