実現したいこと
詳細ページ内にコメント機能を付けたい
発生している問題・分からないこと
Djangoのformのaction属性でテンプレートが読み込まれてしまいます。
エラーメッセージ
error
1TemplateDoesNotExist at /comment/create/1 2pgclub_app/comment_form.html 3Request Method: POST 4Request URL: http://127.0.0.1:8000/comment/create/1 5Django Version: 4.2.1 6Exception Type: TemplateDoesNotExist 7Exception Value: 8pgclub_app/comment_form.html 9 10Internal Server Error: /comment/create/1 11Traceback (most recent call last): 12 File "C:\Users\○○\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner 13 response = get_response(request) 14 ^^^^^^^^^^^^^^^^^^^^^ 15 File "C:\Users\○○\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py", line 220, in _get_response 16 response = response.render() 17 ^^^^^^^^^^^^^^^^^ 18 File "C:\Users\koshi\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\template\response.py", line 114, in render 19 self.content = self.rendered_content 20 ^^^^^^^^^^^^^^^^^^^^^ 21 File "C:\Users\koshi\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\template\response.py", line 90, in rendered_content 22 template = self.resolve_template(self.template_name) 23 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 24 File "C:\Users\○○\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\template\response.py", line 72, in resolve_template 25 return select_template(template, using=self.using) 26 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 27 File "C:\Users\○○\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\template\loader.py", line 47, in select_template 28raise TemplateDoesNotExist(", ".join(template_name_list), chain=chain) 29django.template.exceptions.TemplateDoesNotExist: pgclub_app/comment_form.html
該当のソースコード
question.html(form部)
1<div id = "comment-form" class = "comment-form" style = "display:block"> 2 <p>コメントする</p> 3 <form action = "{% url 'pgclub_app:comment_create' question.pk %}" method = "post" class = "comment-container"> 4 {% csrf_token %} 5 {{ comment_form.text }} 6 <div> 7 <button type = "submit">送信</button> 8 </div> 9 </form> 10</div>
views.py
1class QuestionView(LoginRequiredMixin, DetailView): 2 model = Question 3 template_name = 'question.html' 4 5 def get_context_data(self, **kwargs): 6 context = super().get_context_data(**kwargs) 7 context['comment_form'] = CommentForm 8 context['comment_list'] = self.object.question_target.filter(parent__isnull = True) 9 return context 10 11 12class CreateCommentView(CreateView): 13 model = Comment 14 form_class =CommentForm 15 16 def form_valid(self, form): 17 question_pk = self.kwargs.get('pk') 18 question = get_object_or_404(Question, pk = question_pk) 19 comment = form.save(commit = False) 20 comment.target = question 21 comment.c_user = self.request.user 22 comment.save() 23 return redirect('pgclub_app:question', pk = question_pk)
urls.py
1path('question/<int:pk>/', views.QuestionView.as_view(), name = 'question'), 2path('comment/create/<int:pk>', views.CreateCommentView.as_view(), name = "comment_create"),
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
参考にしたページ
[リンク内容]https://qlitre-weblog.com/django-create-comment-same-page
このページのフォーム部分をそのままコピペして
urlの指定等をこちらのアプリに合わせて改変しても動きませんでした
補足
CommentFormはformsで定義されており、importも正しくされています。
アプリ名はpgclub_appです
もともと動いてたはずなのですが、cssでデザインつけてうちになぜか動かなくなっていました。
追記
一度目のコメントはきちんと動くようですが二度目から動かなくなりました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。