NoReverseMatch at /product_view/1/ 未找到带有参数 '('',)' 的 'cart_add' 的反向。尝试了 1 种模式:[u'cart/add/(?P<pro
Posted
技术标签:
【中文标题】NoReverseMatch at /product_view/1/ 未找到带有参数 \'(\'\',)\' 的 \'cart_add\' 的反向。尝试了 1 种模式:[u\'cart/add/(?P<product_id>\\\\d+)/$\']【英文标题】:NoReverseMatch at /product_view/1/ Reverse for 'cart_add' with arguments '('',)' not found. 1 pattern(s) tried: [u'cart/add/(?P<product_id>\\d+)/$']NoReverseMatch at /product_view/1/ 未找到带有参数 '('',)' 的 'cart_add' 的反向。尝试了 1 种模式:[u'cart/add/(?P<product_id>\\d+)/$'] 【发布时间】:2018-01-22 18:32:03 【问题描述】:我正在创建一个在线商城,但是当我尝试加载我的产品页面时,我收到以下错误。我相信错误指向我的命名空间,但我尝试纠正它的每一种方式,我仍然得到错误
Internal Server Error: /product_view/1/
Traceback (most recent call last):
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/agozie/Desktop/shoppingmall/shop/views.py", line 18, in product_view
'cart_product_form': cart_product_form)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/shortcuts.py", line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/loader.py", line 68, in render_to_string
return template.render(context, request)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/loader_tags.py", line 72, in render
result = block.nodelist.render(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/defaulttags.py", line 216, in render
nodelist.append(node.render_annotated(context))
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/template/defaulttags.py", line 458, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/urls/base.py", line 91, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/urls/resolvers.py", line 497, in _reverse_with_prefix
raise NoReverseMatch(msg)
NoReverseMatch: Reverse for 'cart_add' with arguments '('',)' not found. 1 pattern(s) tried: [u'cart/add/(?P<product_id>\\d+)/$']
[14/Aug/2017 20:29:03] "GET /product_view/1/ HTTP/1.1" 500 186275
我有两个应用程序。购物车和商店。以下是我对购物车应用的看法:
@require_POST
def cart_add(request, product_id):
cart = Cart(request)
product = get_object_or_404(Product, id=product_id)
form = CartAddProductForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cart.add(product=product, quantity=cd['quantity'], update_quantity=cd['update'])
return redirect('cart:cart_detail')
def cart_remove(request, product_id):
cart = Cart(request)
product = get_object_or_404(Product, id=product_id)
cart.remove(product)
return redirect("cart:cart_detail")
def cart_detail(request):
cart = Cart(request)
return render(request, 'cart/detail.html', 'cart':cart)
这里是购物车应用的 urls.py
urlpatterns = [
url(r'^$', views.cart_detail, name='cart_detail'),
url(r'^add/(?P<product_id>\d+)/$', views.cart_add, name='cart_add'),
url(r'^remove/(?P<product_id>\d+)/$', views.cart_remove, name='cart_remove'),
]
还有我项目的 urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('shop.urls')),
url(r'^cart/', include('cart.urls', namespace='cart')),
url(r'^accounts/', include('registration.backends.default.urls')),
]
这是引发错误的模板
% extends 'shop/base.html' %
% load static %
% block title_block %
Product View
% endblock %
% block body_block %
% for p in product %
p.name
p.brand
p.style
<p class="price">$ product.price </p>
<form action="% url 'cart:cart_add' product.id %" method="post">
cart_product_form
% csrf_token %
<input type="submit" value="Add to cart">
</form>
% endfor %
% endblock %
【问题讨论】:
显示出现此错误的模板。 @Daniel Roseman,我已经添加了模板% url 'cart:cart_add' product.id %
% url 'cart:cart_add' product_id=product.id %。
起初我误读了您的urls
文件,并认为您使用了错误的name
,而您并没有,但我还是会指出来。使用命名空间的部分好处是您可以在cart
应用程序中将您的网址命名为add
和remove
,然后在模板中使用cart:add
。
现在我注意到了别的东西。您使用的是product.id
,但它应该是p.id
。
【参考方案1】:
尝试为您的product_id
命名,因为 Django 需要 关键字参数 来解析您的 url。
在您的模板中:
<form acition="% url 'cart:card_add' product_id=p.id %" method="post">
我认为this link会对你有所帮助。
此外,您正在迭代产品% for p in product %
,然后您访问p.name
和其他产品,然后您访问product.price
。尝试将product.id
替换为p.id
(并将product.price
替换为p.price
)。
【讨论】:
我这样做了,现在得到以下错误Reverse for 'cart_add' with keyword arguments 'u'product_id': ''' not found. 1 pattern(s) tried: [u'cart/add/(?P<product_id>\\d+)/$']
那么您可能没有在模板中正确呈现您的product
。
@Gozie 我想我看到了你的错误。您正在迭代product
(% for p in product %
),然后您访问p.name
和其他人,然后您访问product.price
。尝试将product.id
替换为p.id
。
很抱歉,我遇到了另一个错误。我应该将其作为另一个问题发布还是仅将其添加到评论中
最好将其添加为单独的问题并在此处留下链接,我会尽力帮助您。 :)【参考方案2】:
我建议您将product_id
作为id
传递回模板,该模板将为您提供id
。然后你就可以用它来解析反向url了。
@require_POST
def cart_add(request, product_id):
cart = Cart(request)
product = get_object_or_404(Product, id=product_id)
form = CartAddProductForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cart.add(product=product, quantity=cd['quantity'], update_quantity=cd['update'])
return redirect('cart:cart_detail')
% url "product-edit" product_id %
【讨论】:
以上是关于NoReverseMatch at /product_view/1/ 未找到带有参数 '('',)' 的 'cart_add' 的反向。尝试了 1 种模式:[u'cart/add/(?P<pro的主要内容,如果未能解决你的问题,请参考以下文章
NoReverseMatch at ...... in django
NoReverseMatch at / posts / post / 18 / comment / Django Error
Django NoReverseMatch at / 路径名和/或顺序是不是冲突?
NoReverseMatch at / Reverse for 'post_detail' 未找到
NoReverseMatch at /accounts/password-reset/ 用于 Django 中的密码重置
NoReverseMatch at / Reverse for 'singlepropa' 与参数 '('mens-shirt-1',)' 未找到。已尝试 1 种模式