python - NoReverseMatch at /product_view/1/ Reverse for 'cart_add' with arguments '('',)' not found. 1 pattern(s) tried: [u'cart/add/(?P<product_id>\\d+)/$'] -
i creating online shopping mall when try load product page following error. believe error pointing namespace every way try correct it, still error
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 'cart_add' 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
i have 2 apps. cart , shop. here views cart app:
@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})
and here urls.py cart app
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'), ]
and urls.py project
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')), ]
here template error being raised
{% extends 'shop/base.html' %} {% load static %} {% block title_block %} product view {% endblock %} {% block body_block %} {% 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 cart"> </form> {% endfor %} {% endblock %}
try give name product_id
, because django needs keyword arguments resolve url.
in template: <form acition="{% url 'cart:card_add' product_id=p.id %}" method="post">
i think this link helpful you.
also, iterating on product {% p in product %}
, access p.name
, others, access product.price
. try replacing product.id
p.id
(and product.price
p.price
).
Comments
Post a Comment