python=3.7.0
django=(2, 0, 2, 'final', 0)
の環境を使っております。
Djagoチュートリアルを見て投票アプリケーションを作成しています。
投票した後にその投票に紐つけた状態でコメントを書けるように改造を加えようとしていますが試行錯誤していてうまくいきません。
上のページのurl(http://127.0.0.1:8000/polls/6/results/)を保持したままコメントをどうぞのリンクを踏めば紐つけた状態でコメントページに進めると思ったのですがエラーが出てしまいます。
url.pyにpath('add/int:pk/', views.add, name='add'),を表記し、
from django.urls import path from .import views app_name = 'polls' urlpatterns = [ path('', views.IndexView.as_view(), name='view'), path('<int:pk>/', views.DetailView.as_view(), name='detail'), path('<int:pk>/results/', views.ResultsView.as_view(), name='results'), path('<int:question_id>/vote/', views.vote, name='vote'), path('add/<int:pk>/', views.add, name='add'), path('comment/', views.comment, name='comment'), ]
views.pyの関数def add(request, pk):
以下にプライマリーキーを取得できるように関数を作りました。
from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render, redirect from django.urls import reverse from django.views import generic from django.utils import timezone from .forms import CommentForm from .models import Choice, Question, Comment class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_question_list' def get_queryset(self): """Return the last five published questions.""" return Question.objects.order_by('-pub_date')[:5] class DetailView(generic.DetailView): model = Question template_name = 'polls/detail.html' def get_queryset(self): return Question.objects.filter(pub_date__lte=timezone.now()) class ResultsView(generic.DetailView): model = Question template_name = 'polls/results.html' 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,))) def get_queryset(self): """ Return the last five published questions (not including those set to be published in the future). """ return Question.objects.filter( pub_date__lte=timezone.now() ).order_by('-pub_date')[:5] def add(request, pk): #urlのpkを基に、Commentを取得 day = get_object_or_404(Comment, pk=pk) #フォームに取得したCommentを紐つける form = CommentForm(request.POST or None) # method = POST、つまり送信ボタン押下時、入力内容に問題なければ if request.method == 'POST' and form.is_valid(): form.save() return redirect('polls:comment') #通常時のページアクセスや、入力内容に誤りがあればまたページを表示 context = { 'form':form } return render(request, 'polls/day_form.html', context) def comment(request ,pk): context = { 'comment_list':Comment.objects.all(), } return render(request, 'polls/comment_list.html', context)
models.pyのclass Comment(models.Model)は
import datetime from django.db import models from django.utils import timezone class Question(models.Model): question_text = models.CharField('質問内容',max_length=200) pub_date = models.DateTimeField('日付', default=timezone.now) def was_published_recently(self): now = timezone.now() return now - datetime.timedelta(days=1) <= self.pub_date <= now was_published_recently.admin_order_field = 'pub_date' was_published_recently.boolean = True was_published_recently.short_description = 'Published recently?' class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField('投票内容',max_length=200) votes = models.IntegerField('投票数',default=0) class Comment(models.Model): title = models.TextField('コメント') question = models.ForeignKey(Question, verbose_name='紐づく質問', on_delete=models.PROTECT) def __str__(self): return self.text[:10]
forms.pyからフォームを受け取っています。
from django import forms from .models import Comment class CommentForm(forms.ModelForm): class Meta: model = Comment fields = '__all__'
templateフォルダに
basesite.htmlというファイルを設け
{% block branding %} <h1 id="site-name"><a href="{% url 'admin:index' %}">投票アプリケーション</a></h1> {% endblock %}
とだけ記述してあります。
しかしresult.htmlの{% url 'polls:add' question.id %}表記が間違っているらしく最初に書いたエラーが出てしまうのです。
<h1>{{ question.question_text }}現在までの集計</h1> <ul> {% for choice in question.choice_set.all %} <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li> {% endfor %} </ul> <a>投票ありがとうございました</a> <a href="{% url 'polls:add' question.id %}">コメントをどうぞ</a> idを保持したリンクを作れるようにしなくてはいけない
またindexページはこのように記述しており"{% url 'polls:detail' question.id %}"で各々の質問に飛ぶようになっております。このリンクは正常に動作しています。
{% load static %} <link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}"> {% if latest_question_list %} <ul> {% for question in latest_question_list %} <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li> {% endfor %} </ul> {% else %} <p>質問がひとつもありません</p> {% endif %}
投票と紐つけてコメントを書いてもらうという仕組みであれば今の方法にはこだわりません。
参考書籍などでも構いませんのでご教授いただけると幸いです。
回答1件
あなたの回答
tips
プレビュー