如何为django内置评论框架提供删除按钮
我正在使用 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.
There are two issues
- 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 thedjango comments framework
- If there is an
error while submitting the comment form
, the user isredirected 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.
Any help or direction is appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Timmy 的视图代码片段仍然缺少一条导入语句,并且没有返回响应。这是相同的代码,已更新为现在的外部 django_comments 应用程序(django 1.6+):
这将重定向回原始页面,没有任何消息(但大概少了一条评论)。
为该视图注册一个URL:
然后直接修改comments/list.html以包含:
Timmy's code fragment for the view was still missing one import statement and didn't return a response. Here is the same code, updated to the now external django_comments app (django 1.6+):
This will re-direct back to the original page without any message (but, presumably, one comment less).
Register a URL for this view:
And then directly modify comments/list.html to contain:
已经有评论的删除视图,但它是审核系统的一部分。您需要允许所有用户拥有
can_moderate
权限,这显然允许他们删除他们想要删除的任何评论(而不仅仅是他们的)。您可以快速编写自己的视图来检查他们要删除的评论是否属于他们:在您的模板中
对于第二个问题, 你可以看到,如果有错误,重定向总是会发生(即使设置了preview=False)。没有太多的解决方法。您可以创建自己的视图来包装
post_comment
视图(或者只编写您自己的 post_comment 而无需重定向)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:and in your template
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)