简体   繁体   中英

Deleting comments from a post: NoReverseMatch while trying to delete comment of a post

I am quite new to django and web development and trying to delete the comment from posts, and has provided a class based view, but I am getting this error while running the code.

Reverse for 'delete_comment' with no arguments not found. 1 pattern(s) tried: ['posts/(?P<slug>[^/]+)/(?P<comment_id>[0-9]+)/delete/$']

I have provided the comment section below and you can find the delete section towards the last.

comment-section.html

            {{ comments.count }} Comment{{ comments|pluralize }}
        {% for comment in comments %}
        <blockquote class="blockquote">
        <img style="float:left; clear: left;" class="rounded-circle article-img" height="50" width="50" src="{{ comment.user.profile.profile_pic.url }}"><a href="" style="text-decoration: none; color: black;"><h6>{{ comment.user.first_name|capfirst }} {{ comment.user.last_name|capfirst }}</h6></a><br>
        <p style="font-size: 8px;">{{ comment.timestamp }}</p>
        <p style="font-size: 14px;" class="mb-3">{{ comment.content }}</p>
        <a  type="button" name="button" class="reply-btn ml-4"><p style="font-size: 13px;"> Reply</p></a>
        {% if request.user == comment.user %}
        <a href="{% url 'posts:delete_comment' comment.id %}" style="font-size: 13px;text-decoration: none; color: #000;" hover="background-color:red">Delete</a></td>
        {% endif %}

views.py

class DeleteCommentView(BSModalDeleteView, LoginRequiredMixin, UserPassesTestMixin):
    model = Comment
    template_name = 'posts/comment_delete.html'
    success_message = 'Deleted'

    def get_success_url(self):
       return reverse_lazy('posts:detail_post')

    def test_func(self, comment_id):
       comment = self.get_object(self.comment_id)
       if self.request.user == comment.user:
         return True
       return False

urls.py

    path('<slug>/<int:comment_id>/delete/', DeleteCommentView.as_view(), name='delete_comment'),

Please do let me know how can I let users delete their comments from the post. It was easier to delete the whole post.

Thanks in advance!

It looks like your URL takes two arguments, slug and comment_id , but you are only passing in comment_id when you use the {% url %} tag.

It's not altogether obvious what the slug is since you don't show the model, but assuming it's the post slug, you could perhaps fix this with:

{% url 'posts:delete_comment' comment.post.slug comment.id %}

Probably the better solution however is to have your URL not take the slug at all since the ID is enough to identify it. We can't see the code for BSModalDeleteView but I'm guessing it is only expecting an ID, in which case you might need a new URL just for deleting comments such as:

path(
   "/comments/<int:pk>/delete/",
   DeleteCommentView.as_view(),
   name="delete_comment",
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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