我试图传递一个带有多个参数的URL,但是,我一直收到NoReverseMatch错误。我试过打印参数,它们被正确打印。然而,我仍然得到一个错误。
我的观点。
@login_required
def comment_like(request,guid_url,id):
data = dict()
comment = get_object_or_404(Comment, id=id)
user = request.user
if request.method == 'POST':
if comment.likes.filter(id=user.id).exists():
comment.likes.remove(user)
else:
comment.likes.add(user)
data['comment'] = render_to_string('home/posts/comment_like.html',{'comment':comment},request=request)
return JsonResponse(data)
我的链接:
action="{% url 'home:comment-like' post.guid_url comment.id %}"
和我的网址
path('post/<str:guid_url>/comment/<int:id>/like/', views.comment_like, name='comment-like'),
我得到的错误。
django.urls.exceptions.NoReverseMatch: Reverse for 'comment-like' with arguments '('', 20)' not found. 1 pattern(s) tried: ['home/post/(?P<guid_url>[^/]+)/comment/(?P<id>[0-9]+)/like/$']
先谢谢大家的帮助!
在你的路径前加上 "home"。
NoReverseMatch是一个Django异常,当一个URL不能与你的URL配置中的任何字符串或正则表达式匹配时,就会产生异常。请检查你的URL是否正确匹配。
你的模板中存在一个bug。post.guid_url
是空的,正如你的错误的以下部分所提示的那样。
参数'('', 20)'。