购物车应用程序
视图.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/Reverse处找不到参数为“(1,)”和关键字参数为“{}”的“update_cart”的NoReverseMatch。尝试了1个模式:['carts/products/$']请求方法:GET请求URL:http://127.0.0.1:8000/products/Django版本:1.6.5异常类型:NoReverseMatch异常值:找不到具有参数“(1,)”和关键字参数“{}”的“update_cart”的反向。尝试了1个模式:[“$carts/products/$”]异常位置:/Users/apulgupta/Desktop/table1.2/lib/python2.7/site-packages/django/core/urlresolvers。reverse_ with_ prefix中的py,第452行Python可执行文件:/Users/apulgupta/Desktop/table1.2/bin/pythonPythonVersion:2.7.10PythonPath:[“/Users/apulgupta/Desktop/table1.2/table1_2',”/Users/apulgupta/Desktop/table1.2/lib/python27.zip',”/用户/apugupta/桌面/table1.2/lib/ppython2.7',“/Users/apulgutta/桌面/表1.2/lib/python2.7/plat darwin”,“/Users/apulkupta/Desktop/表1.2/库/python2.7/平台mac”,”/Users/apulgupsta/Desktop/表1.2/lib/pyton2.7/plat-mac”,ulgupta/Desktop/table1.2/lib/python2.7/lib tk',“/Users/apulgupta/Desktop/table1.2/lib/python2.7/lib-old',”/Users/apulgupta/Desktop/ttable1.2/lib/python2.7/lib dynload',“/System/Library/Frameworks/Python.Frameworks/Versions/2.7/lib/ppython2.7',”/System/Library/Framworks/Python.framework/Versions/2.7/lib/python2.7/plat darwin',“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac”,“/Systystem/LLibrary/Frameworks/Python.framework/Version/2.7/lib/pyton2.7/plat-mac/lib-scriptpackages”,“/Users/apulgupta/Desktop/table1.2/lib/ppython2.7/site-packages”]服务器时间:2017年2月28日星期二00:23:35 0530模板渲染期间出错
在模板 /Users/apulgupta/Desktop/table1.2/table1_2/templates/base.html 中,第 83 行的错误为“update_cart”,参数为 “(1,)”和关键字参数“{}”,找不到。已尝试 1 种模式: ['$carts/产品/$']
回溯指向问题url模式:“^$carts/products/$”
。您在开头有一个杂乱的$符号,这意味着模式的其余部分将不匹配。
您应该添加正则表达式来捕获 ID。
所以网址应该是
url(r'^cart/products/(?P<product_id>\d+)$', 'carts.views.update_cart', name='update_cart'),
并使用views.py中的id