I face this error even when i add forward slash / to the cart url its give no page found 404
没有反向匹配我认为它是因为url路由它与我一起工作了几秒没有/ for cart url然后它给了我这个错误。
urlpatterns = [
url(r'^', include('shop.urls', namespace='shop')),
url(r'^cart', include('cart.urls', namespace='cart')),
url(r'^admin/', include(admin.site.urls)),
这是视图代码
@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})
购物车应用网址
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.cart_detail, name='cart_detail'),
url(r'^add/(?P<product_id>\d+)/$',views.cart_add,name='cart_add'),
url(r'^remove/(?P<product_id>\d+)/$',views.cart_remove,name='cart_remove'),
]
答案 0 :(得分:0)
from django.urls import reverse
return redirect(reverse('cart:cart_detail'))