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

时间:2017-02-27 19:28:58

标签: python django python-2.7 django-views

购物车应用

views.py

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

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文件

   <p><a href='{% url "update_cart" product.id %}' class="btn btn-primary" role="button">Add to cart</a> </p>

据我所知,urls.py

有一些错误

错误

  

/ products /反向的NoReverseMatch&#39; update_cart&#39;有参数   &#39;(1)&#39;和关键字参数&#39; {}&#39;未找到。尝试过1种模式:   [&#39; carts / products / $&#39;]请求方法:GET请求   URL:http://127.0.0.1:8000/products/ Django版本:1.6.5例外   类型:NoReverseMatch异常值:反向&#39; update_cart&#39;同   论证&#39;(1,)&#39;和关键字参数&#39; {}&#39;未找到。 1种模式   试过:[&#39; $ carts / products / $&#39;]例外   位置:/Users/apulgupta/Desktop/table1.2/lib/python2.7/site-packages/django/core/urlresolvers.py   在_reverse_with_prefix中,第452行Python   可执行文件:/Users/apulgupta/Desktop/table1.2/bin/python Python   版本:2.7.10 Python路径:   [&#39; /Users/apulgupta/Desktop/table1.2/table1_2' ;,   &#39; /Users/apulgupta/Desktop/table1.2/lib/python27.zip' ;,   &#39; /Users/apulgupta/Desktop/table1.2/lib/python2.7' ;,   &#39; /Users/apulgupta/Desktop/table1.2/lib/python2.7/plat-darwin' ;,   &#39; /Users/apulgupta/Desktop/table1.2/lib/python2.7/plat-mac' ;,   &#39; /Users/apulgupta/Desktop/table1.2/lib/python2.7/plat-mac/lib-scriptpackages' ;,   &#39; /Users/apulgupta/Desktop/table1.2/lib/python2.7/lib-tk' ;,   &#39; /Users/apulgupta/Desktop/table1.2/lib/python2.7/lib-old' ;,   &#39; /Users/apulgupta/Desktop/table1.2/lib/python2.7/lib-dynload' ;,   &#39; /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7' ;,   &#39; /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin' ;,   &#39; /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk' ;,   &#39; /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac' ;,   &#39; /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages' ;,   &#39; /Users/apulgupta/Desktop/table1.2/lib/python2.7/site-packages']   服务器时间:2017年2月28日星期二00:23:35 +0530模板期间出错   渲染

     

在模板中   /Users/apulgupta/Desktop/table1.2/table1_2/templates/base.html,错误   在第83行反向&#39; update_cart&#39;参数&#39;(1,)&#39;和关键字   争论&#39; {}&#39;未找到。尝试了1种模式:[&#39; $ carts / products / $&#39;]

2 个答案:

答案 0 :(得分:2)

回溯指向问题网址格式:if not mylist[i] in letters: mylist.remove(mylist[i]) 。您在开始时有一个迷路$符号,这意味着该模式的其余部分将不匹配。

答案 1 :(得分:0)

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

所以网址应为

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

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