On my page, I need to display the message details and the comment form for viewing in order to leave comments. I created 2 general views:
# views.py class PostDetailView (DetailView): model = Post context_object_name = 'post' template_name = 'post.html' def get_context_data(self, **kwargs): context = super(PostDetailView, self).get_context_data(**kwargs) context['comment_form'] = CommentForm() return context class AddCommentView(FormView): template_name = 'post.html' form_class = CommentForm success_url = '/' def form_valid(self, form): form.save() return super(AddCommentView, self).form_valid(form) def form_invalid(self, form): return self.render_to_response(self.get_context_data(form=form)) detail = PostDetailView.as_view() add_comment = AddCommentView.as_view()
The error occurs in AddCommentView, since I did not specify a message identifier for the comment. How can I access post_id in AddCommentView?
source share