简体   繁体   English

如何在帖子中添加评论? PostDetailVew,Django 2.1.5

[英]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网址.py

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

models.py模型.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表格.py

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

views.py视图.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 comment_form.html

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

post_detail.html 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.我认为,“comment_form”需要重定向到“post_detail”,而不是为评论表单生成新页面。

And сould you tell, which parameters has a RichTextField (CKE), how change width, height field only in comment?你能告诉我,哪些参数有 RichTextField (CKE),如何只在评论中改变宽度、高度字段?

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.现在您可以在 html 中添加表单。 And add a submit button to post comment.并添加一个提交按钮来发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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