How can I fix it?
Error message
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']
Error in line 60
<a href="{% url 'add_comment' pk=product.pk %}">add comment</a>
My URLs
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'),
]
My views
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})
Traceback
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 href=" {% url 'add_comment' pk=product.pk %} "> Add comment</a>
61 : {% else %}
62 : <a href="{% url 'index' %}" >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']
I have been searching on the Internet for a week for a solution to this question, but I can't fix it.
CodePudding user response:
Your product.pk
is the empty string, therefore the regex top_cloth/<int:pk>/comment
doesn't match (int
means [0-9]
therefore needs at least one character).
Therefore there is no url found (hence the error).
Now, why your product.pk is empty ? I see you try to render cloth_detail.html
, and in the view you wrote :
product = Comment.objects.order_by()
Your product
is then a list of Comment
? A list has no pk (hence the error).
Let's see your cloth_detail.html
because I'm thinking there is some logic error in there too.