3

I am creating an online shopping mall but when I try to load my product page I get the following error. I believe the error is pointing to my namespace but every way I try to correct it, I still get the 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 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

I have two apps. Cart and Shop. Here is my views for the 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 is the urls.py for the 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 the urls.py for my 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 is the template where the error is being raised

{% 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 %}
Gozie
  • 368
  • 3
  • 19

2 Answers2

3

Try to give a name for your product_id, because Django needs keyword arguments to resolve your url.

In your template: <form acition="{% url 'cart:card_add' product_id=p.id %}" method="post">

I think this link will be helpful for you.

Also, you are iterating over the product {% for p in product %}, then you access p.name and others, but then you access product.price. Try replacing product.id with p.id (and product.price with p.price).

wencakisa
  • 5,850
  • 2
  • 15
  • 36
  • I did that and now get the following error `Reverse for 'cart_add' with keyword arguments '{u'product_id': ''}' not found. 1 pattern(s) tried: [u'cart/add/(?P\\d+)/$']` – Gozie Aug 14 '17 at 20:51
  • Then probably you are not rendering your `product` correctly in the template. – wencakisa Aug 14 '17 at 20:53
  • 1
    @Gozie I think I saw your mistake. You are iterating over `product` (`{% for p in product %}`), then you access `p.name` and others, but then you do `product.price`. Try replacing `product.id` with `p.id`. – wencakisa Aug 14 '17 at 20:55
  • I'm sorry but I'm having another error. Should I post it as another question or just add it in the comment – Gozie Aug 14 '17 at 21:12
  • Better add it as a separate question and leave a link here, I will try to help you. :) – wencakisa Aug 14 '17 at 21:12
  • Seems I can't post a new question till the next 90 minutes – Gozie Aug 14 '17 at 21:20
  • @Gozie Haha, then try to add it here, but I don't think it will be very readable. If you want, add me and text me in Facebook (Vencislav Tasheff ). – wencakisa Aug 14 '17 at 21:23
  • I don't have a facebook account :( Seems I'll just have to wait – Gozie Aug 14 '17 at 21:29
0

I suggest you to pass product_id as id back to the template which will give you id there. Then you can use it to resolve reverse 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 %}
jps
  • 20,041
  • 15
  • 75
  • 79