如何为django内置评论框架提供删除按钮

发布于 2024-12-25 08:29:45 字数 340 浏览 0 评论 0原文

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

有两个问题

  1. 我想为发布的每条评论提供一个删除按钮,并且我不想将用户重定向到另一个页面。我只想删除评论并附上确认消息。我没有找到任何文档告诉我如何在django注释框架中执行此操作
  2. 如果提交评论表单时出现错误,则用户是重定向到预览页面(也处理错误),我不想要这个。我希望用户被重定向到同一页面,并显示适当的错误。我该如何去做这件事呢。

任何帮助或指导表示赞赏

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

  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.

Any help or direction is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惟欲睡 2025-01-01 08:29:45

Timmy 的视图代码片段仍然缺少一条导入语句,并且没有返回响应。这是相同的代码,已更新为现在的外部 django_comments 应用程序(django 1.6+):

from django.shortcuts import get_object_or_404
import django.http as http
from django_comments.views.moderation import perform_delete
from django_comments.models import Comment

def delete_own_comment(request, id):
    comment = get_object_or_404(Comment, id=id)
    if comment.user.id != request.user.id:
        raise Http404
    perform_delete(request, comment)
    return http.HttpResponseRedirect(comment.content_object.get_absolute_url())

这将重定向回原始页面,没有任何消息(但大概少了一条评论)。

为该视图注册一个URL:

 url(r'^comments/delete_own/(?P<id>.*)/

然后直接修改comments/list.html以包含:

{% if user.is_authenticated and comment.user == user %}
   <a href="{% url 'delete_own_comment' comment.id %}">--delete this comment--</a>
{% endif %}
, delete_own_comment, name='delete_own_comment'),

然后直接修改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+):

from django.shortcuts import get_object_or_404
import django.http as http
from django_comments.views.moderation import perform_delete
from django_comments.models import Comment

def delete_own_comment(request, id):
    comment = get_object_or_404(Comment, id=id)
    if comment.user.id != request.user.id:
        raise Http404
    perform_delete(request, comment)
    return http.HttpResponseRedirect(comment.content_object.get_absolute_url())

This will re-direct back to the original page without any message (but, presumably, one comment less).

Register a URL for this view:

 url(r'^comments/delete_own/(?P<id>.*)/

And then directly modify comments/list.html to contain:

{% if user.is_authenticated and comment.user == user %}
   <a href="{% url 'delete_own_comment' comment.id %}">--delete this comment--</a>
{% endif %}
, delete_own_comment, name='delete_own_comment'),

And then directly modify comments/list.html to contain:

蹲在坟头点根烟 2025-01-01 08:29:45

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

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 而无需重定向)

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)

and in your template

{% 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 %}

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文