如何为内置注释框架的django提供删除按钮 [英] How to provide a delete button for django built in comments framework

查看:54
本文介绍了如何为内置注释框架的django提供删除按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django评论框架.它说它提供了很多功能,而且我还可以在源文件中看到有很多选项,但是文档有点差.

I am using django comment framework. It says it provides a lot of functionality, and I can also see in the source files that there are various options, but the documentation is a bit poor.

有两个问题

  1. 我想为发布的每个评论提供一个删除按钮,并且我不想将用户重定向到另一个页面.我只想删除该评论并附带一条确认消息.我还没有找到任何文档告诉我如何在 django评论框架
  2. 中做到这一点.
  3. 如果提交评论表单时出现错误,则用户被重定向到预览页面(也处理错误),我不希望这样做.我希望将用户重定向到同一页面,并显示相应的错误.我该怎么做.
  1. I want to provide a delete button for each comment that is posted, and I don't want to redirect the user to another page. I just want the comment to be deleted with a confirmation message. I have not found any documentation that tells me how I can do this in the django comments framework
  2. If there is an error while submitting the comment form, the user is redirected to the preview page(that handles errors also), I don't want this. I want the user to be redirected to the same page, with the appropriate errors. How can I go about doing this.

感谢任何帮助或指导

推荐答案

已经有一个删除评论的视图,但它是审阅系统的一部分.您需要允许所有用户具有 can_moderate 权限,这显然将使他们能够删除他们想要的任何评论(而不仅仅是他们的评论).您可以快速编写自己的视图,以检查他们正在删除的评论是否属于他们:

There is already a delete view for comments but it is a part of the moderation system. You would need to allow all users the can_moderate permission which would obviously allow them to remove any comment they want (not just theirs). You can quickly write your own view that checks that the comment they are deleting belongs to them:

from django.shortcuts import get_object_or_404
from django.contrib.comments.view.moderate import perform_delete
def delete_own_comment(request, comment_id):
    comment = get_object_or_404(Comment, id=comment_id)
    if comment.user.id != request.user.id:
        raise Http404
    perform_delete(request, comment)

并在您的模板中

{% for comment in ... %}
{% if user.is_authenticated and comment.user == user %}
    {% url path.to.view.delete_comment comment_id=comment.id as delete_url %}
    <a href="{{ delete_url }}">delete your comment</a>
{% endif %}
{% endfor %}

对于第二个问题,您可以看到,如果有错误,重定向将始终发生(即使设置了Preview = False).没有太多的解决方法.您可以创建自己的视图来包装 post_comment 视图(或只写自己的post_comment而无需重定向)

For the second problem, you can see that the redirection will always happen if there are errors (even if preview=False is set). There aren't too many workarounds. You could create your own view that wraps the post_comment view (or just write your own post_comment withouth the redirection)

这篇关于如何为内置注释框架的django提供删除按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆