[Fixed]-How can I implement FormView into DetailView, django

1๐Ÿ‘

โœ…

I would not try to mix logic for both usecase in a single view.

class BlogFullPostView(DetailView):
    model = Post
    template_name = 'full_post.html'
    pk_url_kwarg = 'post_id'
    context_object_name = 'post'

    def get_context_data(self, **kwargs):
        context = super(BlogFullPostView, self).get_context_data(**kwargs)
        context['comments'] = Comment.objects.filter(post=self.object)
        context['form'] = AddCommentForm(initial={'post': self.object })
        return context

class CommentFormView(FormView):
    form_class = AddCommentForm

    def get_success_url(self):
        # logic here for post url 


# full_post.html

<form method="post" action="{% url "comment_form_view_url" %}">
    {{ form }}
</form>
๐Ÿ‘คmishbah

Leave a comment