简体   繁体   English

在帖子详细信息页面上插入评论表单。 django-博客应用

[英]Insert comment form on Post detail page. django- blog app

I have trying this from 2-3 days.我已经尝试了 2-3 天。 I want to insert my comment form on post detail page.我想在帖子详细信息页面上插入我的评论表单。 My form is not showing on that page.我的表格没有显示在该页面上。 I have followed this tutorial for the comment model.我已经按照本教程的评论 model。 I have another app for account which is used for signup purpose.我有另一个用于注册目的的帐户应用程序。 model.py model.py

        class Post(models.Model):
        author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
        title = models.CharField(max_length=200)
        text = models.TextField()
        created_date = models.DateTimeField(default=timezone.now)
        published_date = models.DateTimeField(blank=True, null=True)
        def __str__(self):
                return self.title
        def publish(self):
                self.published_date = timezone.now()
                self.save()
        def approved_comments(self):
            return self.comments.filter(approved_comment=True)       

class Comment(models.Model):
        post = models.ForeignKey('Post', on_delete=models.CASCADE, related_name='comments')
        author = models.CharField(verbose_name ='Username',max_length=200)
        text = models.TextField(verbose_name ='Comment')
        email = models.EmailField(default='')
        created_date = models.DateTimeField(default=timezone.now)
        approved_comment = models.BooleanField(default=False)

        def approve(self):
            self.approved_comment = True
            self.save()

        def __str__(self):
            return self.text

view.py视图.py

def post_detail(request, pk):

    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})  


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.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'blog/add_comment_to_post.html', {'form': form})

@login_required
def comment_approve(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    comment.approve()
    return redirect('post_detail', pk=comment.post.pk)

@login_required
def comment_remove(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    comment.delete()
    return redirect('post_detail', pk=comment.post.pk)

urls.py网址.py

path('post/<int:pk>/comment/', views.add_comment_to_post, name='add_comment_to_post'),
    path('comment/<int:pk>/approve/', views.comment_approve, name='comment_approve'),
    path('comment/<int:pk>/remove/', views.comment_remove, name='comment_remove'),
    path('post/<int:pk>/', views.post_detail, name='post_detail'),

Please help me out to find a way to insert it in my post_deatil.html.请帮助我找到将其插入我的 post_deutil.html 的方法。

In case you want the same page to see the post and also add a comment you only need one view like:如果您希望同一页面查看帖子并添加评论,您只需要一个视图,例如:

def post_details(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.save()
        return redirect('post_detail', pk=post.pk)
else:
    form = CommentForm()
return render(request, 'blog/post_details.html', {'form': form, 'post': post})

Then your url will be like:那么您的 url 将如下所示:

path('post/<int:pk>/', views.post_details, name='post-details'),

And finally you have to mix both of your templates, first showing the information of the post and on the bottom adding the form.最后你必须混合你的两个模板,首先显示帖子的信息,然后在底部添加表单。

With that you will be able to delete the add_comment_to_post view and url.这样您就可以删除add_comment_to_post视图和 url。

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

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