简体   繁体   中英

How can I display my comment form on the post detail page Django

So basically at the moment, if users want to add a comment to a post on my site, it takes them to another page with the form on it. However, I want the comment form to be on the actual Post Detail page, so users don't have to go to another page to comment.

So far I have tried adding some context things and changed the url for the comment form's location to post_detail.html , and put the comment_form.html 's code there, however this did not work.

Here are the relevant views.py The add_comment_to_post view

@login_required(login_url='/mainapp/user_login/')
def add_comment_to_post(request,pk):
    post = get_object_or_404(Post,pk=pk)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.author = request.user # add this line
            comment.save()
            return redirect('mainapp:post_detail',pk=post.pk)
            # remove `def form_valid`
    else:
        form = CommentForm()
    return render(request,'mainapp/comment_form.html',{'form':form})

Here is the PostDetailView view.

class PostDetailView(DetailView):
    model = Post

Here is the comment_form.html code

<form class="post-form" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit" class="submitbtn">Comment</button>
</form>

And here is the relevant urls.py files

path('post/<int:pk>/comment/', views.add_comment_to_post, name='add_comment_to_post'),

path('post/<int:pk>', views.PostDetailView.as_view(), name='post_detail'),

So at the moment, when doing my solutions I thought would work, I added the comment_form.html's code into the post_detail.html document, however it only showed the Comment html button. How am I able to have the CommentForm on the same page as the post detail page?

Thanks for any help:)

The problem is that when Django is rendering the PostDetailView , the context dict doesn't have the form item (the form item is only available at your add_comment_to_post view, since Django template engine cannot find the form item from context dict, it did not render anything.

What you need to do is to change your PostDetailView and to inject the CommentForm into the PostDetailView 's context. Here is one way to do it:

class PostDetailView(DetailView):
        model = Post

        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            context['form'] = CommentForm() # Inject CommentForm
            return context

What you have done is essentially overwriting the default get_context_data , and inject that CommentForm() of yours as part of the context , and then render it

You can try like this:

class PostDetailView(DetailView):
        model = Post

        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            context['comment_form'] = YourModelFormForComment()  # Your comment form
            return context

In template

{{comment_form.as_p}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM