【问题标题】:How to add a comment to post? PostDetailVew, Django 2.1.5如何在帖子中添加评论? PostDetailVew,Django 2.1.5
【发布时间】:2019-01-15 05:57:07
【问题描述】:

我想添加评论以在他的页面上发布(“发布详细信息”页面)。

我找到了答案,但它在其他页面上创建了评论。我想在“帖子详细信息”页面上创建评论。

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>
...

我认为,“comment_form”需要重定向到“post_detail”,而不是为评论表单生成新页面。

您能告诉我,哪些参数有 RichTextField (CKE),如何仅在注释中更改宽度、高度字段?

【问题讨论】:

    标签: python django


    【解决方案1】:

    如果您想在详细信息页面中直接添加评论表单,那么您所要做的就是在您的视图中添加表单和发布功能,

    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)
    

    现在您可以在 html 中添加表单。并添加一个提交按钮来发表评论。

    【讨论】:

    • 不工作。我将您的代码添加到视图中,删除了 CommentCreate添加了代码"comment_from.html" “post_detail.html”。结果,页面只添加了“提交”按钮,没有文本区域。
    • 你用过{% for form in commentform %}这条线吗?
    • 对不起。这是工作。只需将comment form中的form.as_table改为commentform.as_table即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    相关资源
    最近更新 更多