Django - Catch argument in FormView class

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() # urls.py .... url(r'^(?P<pk>\d+)/$', view='detail'), url(r'^(?P<post_id>\d+)/add_comment/$', view='add_comment'), .... 

The error occurs in AddCommentView, since I did not specify a message identifier for the comment. How can I access post_id in AddCommentView?

+6
source share
1 answer

self.kwargs['post_id'] or self.args[0] contains this value

Docs

+18
source

Source: https://habr.com/ru/post/914644/


All Articles