python - 尝试使用 django 制作购物车,但在 View 中我传递的是产品 id 而不是 slug,因为我直接从主页将产品添加到购物车

标签 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 中有一些错误

错误

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/$'。开头有一个杂散的 $ 符号,这意味着模式的其余部分将不匹配。

关于python - 尝试使用 django 制作购物车,但在 View 中我传递的是产品 id 而不是 slug,因为我直接从主页将产品添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42494156/

相关文章:

python - 导入两个同名包

django - 如何在 nginx + django + kubernetes 中提供静态内容?

python - 单击youtube按钮 Selenium python检查我的代码

python - 如何在 Python 中进行 scp?

python - Pandas 错误转义 %s"% escape, len(escape)

python - 在 Sphinx 自动摘要中使用第一段而不是第一行

python - Python 中的二叉树

django - 在 Django 中命名基于类的 View 有错误的方法吗?

python - 如何在 Python 中循环调用函数

python - 当我明确提供键作为第一个元素时,为什么排序 python 元组列表更快?