문제

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

도움이 되었습니까?

해결책

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)

다른 팁

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>.*)/$', delete_own_comment, name='delete_own_comment'),

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 %}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top