反向 'add_comment' 关键字参数 '{'pk': ''}' 未找到。尝试了 2 种模式:
Reverse for 'add_comment' with keyword arguments '{'pk': ''}' not found. 2 pattern(s) tried:
我该如何解决?
错误信息
Reverse for 'add_comment' with keyword arguments '{'pk': ''}' not found. 2 pattern(s) tried: ['shopping_mall/top_cloth/(?P[0-9]+)/comment\Z', 'top_cloth/(?P[0-9]+)/comment\Z']
第 60 行错误
<a class="btn btn-success" href="{% url 'add_comment' pk=product.pk %}">add comment</a>
我的网址
urlpatterns = [
path('', views.index, name='index'),
path('top_cloth/', views.top_cloth),
path('top_cloth/<int:pk>/', views.cloth_detail, name='top_cloth_pk'),
path('top_cloth/<int:pk>/comment', views.add_comment, name='add_comment'),
path('top_cloth/<int:pk>/remove/', views.comment_remove, name='comment_remove'),
path('top_cloth/<int:pk>/modify/', views.comment_modify, name='comment_modify'),
]
我的观点
def cloth_detail(request, pk):
post = ProductList.objects.get(pk=pk)
product = Comment.objects.order_by()
return render(
request,
'shopping_mall/cloth_detail.html',
{
'post': post, 'product': product
}
)
def add_comment(request, pk):
product = get_object_or_404(ProductList, pk=pk)
if request.method == "POST":
form = CommentFrom(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.product = product
comment.save()
return redirect('index', pk=product.pk)
else:
form = CommentFrom()
return render(request, 'shopping_mall/add_comment.html', {'form':form})
回溯
Request Method: GET
Request URL: http://127.0.0.1:8000/shopping_mall/top_cloth/1/
Django Version: 4.0.4
Python Version: 3.9.7
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'crispy_forms',
'shopping_mall',
'single_pages',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'django_summernote']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template D:\github\kys-shoppingmall\shopping_mall\templates\shopping_mall\cloth_detail.html, error at line 60
Reverse for 'add_comment' with keyword arguments '{'pk': ''}' not found. 2 pattern(s) tried: ['shopping_mall/top_cloth/(?P<pk>[0-9]+)/comment\Z', 'top_cloth/(?P<pk>[0-9]+)/comment\Z']
50 : {{ comment.text|linebreaks }}
51 : </p>
52 : </dlv>
53 : {% endif %}
54 :
55 : {% empty %}
56 : <p>Not yet comment</p>
57 : {% endfor %}
58 : {% if user.is_authenticated %}
59 :
60 : <a class="btn btn-success" href=" {% url 'add_comment' pk=product.pk %} "> Add comment</a>
61 : {% else %}
62 : <a href="{% url 'index' %}" class="btn btn-danger">please login</a>
63 : {% endif %}
64 : </section>
65 : {% endblock %}
Traceback (most recent call last):
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\github\kys-shoppingmall\shopping_mall\views.py", line 29, in cloth_detail
return render(
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\shortcuts.py", line 24, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\backends\django.py", line 62, in render
return self.template.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
return self.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\loader_tags.py", line 157, in render
return compiled_parent._render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
return self.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\loader_tags.py", line 63, in render
result = block.nodelist.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
return self.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\defaulttags.py", line 322, in render
return nodelist.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
return self.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\defaulttags.py", line 472, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\urls\base.py", line 88, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\urls\resolvers.py", line 802, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /shopping_mall/top_cloth/1/
Exception Value: Reverse for 'add_comment' with keyword arguments '{'pk': ''}' not found. 2 pattern(s) tried: ['shopping_mall/top_cloth/(?P<pk>[0-9]+)/comment\Z', 'top_cloth/(?P<pk>[0-9]+)/comment\Z']
我在网上找了一个星期来解决这个问题,但我无法解决。
您的 product.pk
是空字符串,因此正则表达式 top_cloth/<int:pk>/comment
不匹配(int
表示 [0-9]+
因此至少需要一个字符)。
因此没有找到 url(因此出现错误)。
现在,为什么您的 product.pk 是空的?我看到您尝试渲染 cloth_detail.html
,并且在您写的视图中:
product = Comment.objects.order_by()
您的 product
是 list of Comment
吗?列表没有主键(因此出现错误)。
让我们看看你的 cloth_detail.html
因为我认为那里也有一些逻辑错误。
我该如何解决?
错误信息
Reverse for 'add_comment' with keyword arguments '{'pk': ''}' not found. 2 pattern(s) tried: ['shopping_mall/top_cloth/(?P[0-9]+)/comment\Z', 'top_cloth/(?P[0-9]+)/comment\Z']
第 60 行错误
<a class="btn btn-success" href="{% url 'add_comment' pk=product.pk %}">add comment</a>
我的网址
urlpatterns = [
path('', views.index, name='index'),
path('top_cloth/', views.top_cloth),
path('top_cloth/<int:pk>/', views.cloth_detail, name='top_cloth_pk'),
path('top_cloth/<int:pk>/comment', views.add_comment, name='add_comment'),
path('top_cloth/<int:pk>/remove/', views.comment_remove, name='comment_remove'),
path('top_cloth/<int:pk>/modify/', views.comment_modify, name='comment_modify'),
]
我的观点
def cloth_detail(request, pk):
post = ProductList.objects.get(pk=pk)
product = Comment.objects.order_by()
return render(
request,
'shopping_mall/cloth_detail.html',
{
'post': post, 'product': product
}
)
def add_comment(request, pk):
product = get_object_or_404(ProductList, pk=pk)
if request.method == "POST":
form = CommentFrom(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.product = product
comment.save()
return redirect('index', pk=product.pk)
else:
form = CommentFrom()
return render(request, 'shopping_mall/add_comment.html', {'form':form})
回溯
Request Method: GET
Request URL: http://127.0.0.1:8000/shopping_mall/top_cloth/1/
Django Version: 4.0.4
Python Version: 3.9.7
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'crispy_forms',
'shopping_mall',
'single_pages',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'django_summernote']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template D:\github\kys-shoppingmall\shopping_mall\templates\shopping_mall\cloth_detail.html, error at line 60
Reverse for 'add_comment' with keyword arguments '{'pk': ''}' not found. 2 pattern(s) tried: ['shopping_mall/top_cloth/(?P<pk>[0-9]+)/comment\Z', 'top_cloth/(?P<pk>[0-9]+)/comment\Z']
50 : {{ comment.text|linebreaks }}
51 : </p>
52 : </dlv>
53 : {% endif %}
54 :
55 : {% empty %}
56 : <p>Not yet comment</p>
57 : {% endfor %}
58 : {% if user.is_authenticated %}
59 :
60 : <a class="btn btn-success" href=" {% url 'add_comment' pk=product.pk %} "> Add comment</a>
61 : {% else %}
62 : <a href="{% url 'index' %}" class="btn btn-danger">please login</a>
63 : {% endif %}
64 : </section>
65 : {% endblock %}
Traceback (most recent call last):
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\github\kys-shoppingmall\shopping_mall\views.py", line 29, in cloth_detail
return render(
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\shortcuts.py", line 24, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\backends\django.py", line 62, in render
return self.template.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
return self.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\loader_tags.py", line 157, in render
return compiled_parent._render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
return self.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\loader_tags.py", line 63, in render
result = block.nodelist.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
return self.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\defaulttags.py", line 322, in render
return nodelist.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
return self.render(context)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\template\defaulttags.py", line 472, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\urls\base.py", line 88, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "D:\github\kys-shoppingmall\venv\lib\site-packages\django\urls\resolvers.py", line 802, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /shopping_mall/top_cloth/1/
Exception Value: Reverse for 'add_comment' with keyword arguments '{'pk': ''}' not found. 2 pattern(s) tried: ['shopping_mall/top_cloth/(?P<pk>[0-9]+)/comment\Z', 'top_cloth/(?P<pk>[0-9]+)/comment\Z']
我在网上找了一个星期来解决这个问题,但我无法解决。
您的 product.pk
是空字符串,因此正则表达式 top_cloth/<int:pk>/comment
不匹配(int
表示 [0-9]+
因此至少需要一个字符)。
因此没有找到 url(因此出现错误)。
现在,为什么您的 product.pk 是空的?我看到您尝试渲染 cloth_detail.html
,并且在您写的视图中:
product = Comment.objects.order_by()
您的 product
是 list of Comment
吗?列表没有主键(因此出现错误)。
让我们看看你的 cloth_detail.html
因为我认为那里也有一些逻辑错误。