Skip to content
Advertisement

How to add a comment to post? PostDetailVew, Django 2.1.5

I want add comment to post on his page (“post detail” page).

I was find answer, but it create comment on other page. I want create comment on page of “post detail”.

urls.py

url(r'^post/(?P<pk>d+)/create/$', views.CommentCreate.as_view(), name='comment_create'),

models.py

class Comment(models.Model):
    description = RichTextUploadingField()
    author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    comment_date = models.DateTimeField(auto_now_add=True, null=True)
    post = models.ForeignKey(Post, on_delete=models.CASCADE)

    class Meta:
        ordering = ["-comment_date"]

    def __str__(self):
        return "{}".format(self.description)

forms.py

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ['description']

views.py

class PostDetailView(generic.DetailView):
    model = Post


class CommentCreate(LoginRequiredMixin, CreateView):
    model = Comment
    fields = ['description']

    def get_context_data(self, **kwargs):
        context = super(CommentCreate, self).get_context_data(**kwargs)
        context['post'] = get_object_or_404(Post, pk = self.kwargs['pk'])
        return context

    def form_valid(self, form):
        form.instance.author = self.request.user
        form.instance.post=get_object_or_404(Post, pk = self.kwargs['pk'])
        return super(CommentCreate, self).form_valid(form)

    def get_success_url(self):
        return reverse('post-detail', kwargs={'pk': self.kwargs['pk'],})

comment_form.html

...    
<form action="" method="post">
    {% csrf_token %}
    <table>
    {{ form.as_table }}
    </table>
    <button type="submit">Submit</button>
</form>
...

post_detail.html

...
{% for comment in post.comment_set.all %}
<p>{{ comment.author }} ({{ comment.comment_date }}) {{ comment.description|safe }}</p>
{% endfor %}
<hr>
{% if user.is_authenticated %}
<p><a href = "{% url 'comment_create' post.id %}">Add a new comment</a></p>
...

I think, “comment_form” need to redirect to “post_detail”, not generate new page for comment form.

And сould you tell, which parameters has a RichTextField (CKE), how change width, height field only in comment?

Advertisement

Answer

If you want the comment form right in your detail page then all you have to do is to add the form and post function in your View,

class PostDetailView(DetailView):
    model = Post
    template_name = 'yourdetailpage.html'

    def get_context_data(self, **kwargs):
       context = super(PostDetailView, self).get_context_data(**kwargs)
       context['commentform'] = CommentForm()
       return context

    def post(self, request, pk):
       post = get_object_or_404(Post, pk=pk)
       form = CommentForm(request.POST)

       if form.is_valid():
           obj  = form.save(commit=False)
           obj.post = post
           obj.author = self.request.user
           obj.save()
           return redirect('detail', post.pk)

And now you can add your form in your html. And add a submit button to post comment.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement