首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >我的django 2.0.5商店项目没有反向匹配url错误

我的django 2.0.5商店项目没有反向匹配url错误
EN

Stack Overflow用户
提问于 2018-11-19 21:55:34
回答 1查看 450关注 0票数 0

我工作在一个django 2.0.5项目建立一个电子商务商店使用django商店,我得到一个没有反向匹配的网址错误

代码语言:javascript
代码运行次数:0
运行
复制
Reverse for 'cart_add' with arguments '('',)' not found. 1 pattern(s) tried: 
['cart\\/add\\/(?P<product_id>[0-9]+)\\/$']

myshop/cart/urls.py

代码语言:javascript
代码运行次数:0
运行
复制
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

代码语言:javascript
代码运行次数:0
运行
复制
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

代码语言:javascript
代码运行次数:0
运行
复制
{% 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

代码语言:javascript
代码运行次数:0
运行
复制
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的例子,一开始程序是工作的,我只是不知道是什么改变了,我不能跟踪错误

EN

回答 1

Stack Overflow用户

发布于 2018-11-19 22:13:42

您没有在{% 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 %}

也不需要将您的urls称为cart_addcart_detail。它们已经在cart名称空间中,只需将它们称为adddetail

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53376176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档