我的django 2.0.5 shop项目没有反向匹配URL错误

时间:2018-11-19 13:55:34

标签: python django

我正在django 2.0.5项目上使用django商店建立电子商务商店,却没有反向匹配网址错误

Reverse for 'cart_add' with arguments '('',)' not found. 1 pattern(s) tried: 
['cart\\/add\\/(?P<product_id>[0-9]+)\\/$']

myshop / cart / urls.py

from django.urls import path
from . import views
app_name = 'cart'
urlpatterns = [
    path('', views.cart_detail, name='cart_detail'),
    path('add/<int:product_id>/', views.cart_add, name='cart_add'),
    path('remove/<int:product_id>/', views.cart_remove, name='cart_remove'),

]

myshop / cart / views.py

from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_POST
from shop.models import Product
from .cart import Cart
from .forms import CartAddProductForm
@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)
    for item in cart:
        item['update_quantity_form'] = CartAddProductForm(
                          initial={'quantity': item['quantity'],
                          'update': True})
    return render(request, 'cart/detail.html', {'cart': cart})

myshop / cart / templates / cart / detail.html

{% extends "shop/base.html" %}
{% load static %}

{% block title %}
  Your shopping cart
{% endblock %}

{% block content %}
  <h1>Your shopping cart</h1>
  <table class="cart">
    <thead>
      <tr>
        <th>Image</th>
        <th>Product</th>
        <th>Quantity</th>
        <th>Remove</th>
        <th>Unit price</th>
        <th>Price</th>
      </tr>
    </thead>
    <tbody>
      {% for item in cart %}
        {% with product=item.product %}
          <tr>
            <td>
              <a href="{{ product.get_absolute_url }}">
                <img src="{% if product.image %}{{ product.image.url }}{% 
else %}{% static "img/no_image.png" %}{% endif %}">
              </a>
            </td>
            <td>{{ product.name }}</td>
            <td>
              <form action="{% url "cart:cart_add" product.id %}" method="post">
                {{ item.update_quantity_form.quantity }}
                 {{ item.update_quantity_form.update }}
                 <input type="submit" value="Update">
                 {% csrf_token %}
              </form>
             </td>
             <td><a href="{% url "cart:cart_remove" product.id 
%}">Remove</a></td>
            <td class="num">${{ item.price }}</td>
            <td class="num">${{ item.total_price }}</td>
          </tr>
        {% endwith %}
       {% endfor %}
      <tr class="total">
        <td>Total</td>
        <td colspan="4"></td>
        <td class="num">${{ cart.get_total_price }}</td>
       </tr>
    </tbody>
  </table>
  <p class="text-right">
    <a href="{% url "shop:product_list" %}" class="button light">Continue 
shopping</a>
    <a href="{% url "orders:order_create" %}" class="button">
      Checkout
   </a>
  </p>
{% endblock %}

myshop / urls.py

from django.conf import settings
from django.conf.urls.static import static

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('orders/', include('orders.urls', namespace='orders')),
    path('cart/', include('cart.urls', namespace='cart')),
    path('', include('shop.urls', namespace='shop')),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

我正在做这本书,例如django 2,最初程序正在运行,我只是不知道发生了什么变化,而且我无法跟踪错误

1 个答案:

答案 0 :(得分:0)

您在{% url "cart:cart_add" product.id %}中未提供任何ID,您已经提到了参数的名称。 尝试{% url "cart:cart_add" product.id=product.id %}

最好将URL模式中的product.id重命名为id,以提高可读性。然后使用{%url“ cart:cart_add” id = product.id%}

也无需调用您的网址cart_addcart_detail。它们已经在cart命名空间中,只需将它们命名为adddetail