关于python:尝试使用django制作购物车,但在视图中我传递产品ID而不是slug因为我直接从主页添加产品到购物车

Trying to make a cart using django but in views I am passing product id rather than slug because I am adding product to cart directly from homepage

购物车应用

views.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from django.shortcuts import render, HttpResponseRedirect
from django.core.urlresolvers import reverse
# Create your views here.

from products.models import Product
from .models import Cart

def view(request):
   cart = Cart.objects.all()[0]
   context = {"cart": cart}
   template ="cart/view.html"
   return render(request, template, context)

def update_cart(request, id):
   cart = Cart.objects.all()[0]
   try:
       product = Product.objects.get(id=id)
   except Product.DoesNotExist:
       pass
   except:
       pass
   if not product in cart.products.all():
       cart.products.add(product)
   else:
       cart.products.remove(product)

   return HttpResponseRedirect(reverse("cart"))

urls.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
   # Examples:
    url(r'^$', 'products.views.home', name='home'),
    url(r'^products/$', 'products.views.home', name='products'),
    url(r'^cart/products/$', 'carts.views.update_cart', name='update_cart'),
    url(r'^cart/$', 'carts.views.view', name='cart'),
    url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

base.html文件

1
2
3
   <p>
Add to cart
</p>

据我说,urls.py有一些错误

错误

NoReverseMatch at /products/ Reverse for 'update_cart' with arguments
'(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried:
['carts/products/$'] Request Method: GET Request
URL: http://127.0.0.1:8000/products/ Django Version: 1.6.5 Exception
Type: NoReverseMatch Exception Value: Reverse for 'update_cart' with
arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s)
tried: ['$carts/products/$'] Exception
Location: /Users/apulgupta/Desktop/table1.2/lib/python2.7/site-packages/django/core/urlresolvers.py
in _reverse_with_prefix, line 452 Python
Executable: /Users/apulgupta/Desktop/table1.2/bin/python Python
Version: 2.7.10 Python Path:
['/Users/apulgupta/Desktop/table1.2/table1_2',
'/Users/apulgupta/Desktop/table1.2/lib/python27.zip',
'/Users/apulgupta/Desktop/table1.2/lib/python2.7',
'/Users/apulgupta/Desktop/table1.2/lib/python2.7/plat-darwin',
'/Users/apulgupta/Desktop/table1.2/lib/python2.7/plat-mac',
'/Users/apulgupta/Desktop/table1.2/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/apulgupta/Desktop/table1.2/lib/python2.7/lib-tk',
'/Users/apulgupta/Desktop/table1.2/lib/python2.7/lib-old',
'/Users/apulgupta/Desktop/table1.2/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/apulgupta/Desktop/table1.2/lib/python2.7/site-packages']
Server time: Tue, 28 Feb 2017 00:23:35 +0530 Error during template
rendering

In template
/Users/apulgupta/Desktop/table1.2/table1_2/templates/base.html, error
at line 83 Reverse for 'update_cart' with arguments '(1,)' and keyword
arguments '{}' not found. 1 pattern(s) tried: ['$carts/products/$']


回溯指向问题url模式:'^$carts/products/$'。 你在开始时有一个迷路的$符号,这意味着该模式的其余部分将不匹配。


您应该添加正则表达式来捕获ID。

所以URL应该是

1
url(r'^cart/products/(?P<product_id>\d+)$', 'carts.views.update_cart', name='update_cart'),

在urls.py中并使用views.py中的id