删除 post 的评论:NoReverseMatch 尝试删除 post 的评论
Deleting comments from a post: NoReverseMatch while trying to delete comment of a post
我对 django 和 web 开发还很陌生,并试图从 posts 中删除评论,并提供了一个基于 class 的视图,但我在 [=36] 时收到此错误=]代码。
Reverse for 'delete_comment' with no arguments not found. 1 pattern(s) tried: ['posts/(?P<slug>[^/]+)/(?P<comment_id>[0-9]+)/delete/$']
我在下面提供了评论部分,您可以在最后找到删除部分。
评论-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'),
请告诉我如何让用户从 post 中删除他们的评论。删除整个 post.
更容易
提前致谢!
看起来你的 URL 有两个参数,slug
和 comment_id
,但你只在使用 {% url %}
时传入 comment_id
标签。
由于您没有显示模型,所以这个弹头是什么并不完全明显,但假设它是 post 弹头,您也许可以通过以下方式解决此问题:
{% url 'posts:delete_comment' comment.post.slug comment.id %}
不过,可能更好的解决方案是让您的 URL 完全不带鼻涕虫,因为 ID 足以识别它。我们看不到 BSModalDeleteView
的代码,但我猜它只需要一个 ID,在这种情况下,您可能需要一个新的 URL 来删除评论,例如:
path(
"/comments/<int:pk>/delete/",
DeleteCommentView.as_view(),
name="delete_comment",
)
我对 django 和 web 开发还很陌生,并试图从 posts 中删除评论,并提供了一个基于 class 的视图,但我在 [=36] 时收到此错误=]代码。
Reverse for 'delete_comment' with no arguments not found. 1 pattern(s) tried: ['posts/(?P<slug>[^/]+)/(?P<comment_id>[0-9]+)/delete/$']
我在下面提供了评论部分,您可以在最后找到删除部分。
评论-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'),
请告诉我如何让用户从 post 中删除他们的评论。删除整个 post.
更容易提前致谢!
看起来你的 URL 有两个参数,slug
和 comment_id
,但你只在使用 {% url %}
时传入 comment_id
标签。
由于您没有显示模型,所以这个弹头是什么并不完全明显,但假设它是 post 弹头,您也许可以通过以下方式解决此问题:
{% url 'posts:delete_comment' comment.post.slug comment.id %}
不过,可能更好的解决方案是让您的 URL 完全不带鼻涕虫,因为 ID 足以识别它。我们看不到 BSModalDeleteView
的代码,但我猜它只需要一个 ID,在这种情况下,您可能需要一个新的 URL 来删除评论,例如:
path(
"/comments/<int:pk>/delete/",
DeleteCommentView.as_view(),
name="delete_comment",
)