Django 给出错误"The current path, item/14/add_to_cart1/, didn't match any of these."



我正试图在我的django产品中添加添加到购物车功能。首先我有项目的列表视图,然后我有详细信息视图,然后根据项目id将项目添加到购物车中。我的urls.py如下:

url('logout/', views.logout, name="logout"),
url('^contact$', views.contact, name='contact'),
url('^signup', views.signup, name="signup"),
# url('^index$', views.index, name = 'index'),
url('^about$', views.about, name='about'),
url('^demo', views.demo, name='demo'),
url('^payment', views.payment, name='payment'),
url('logout', auth_views.LogoutView.as_view(), name='logout'),
url(r'^accounts/', include('registration.backends.default.urls')),
path('detail', views.detai, name='detai'),
url('^$', views.home,name = 'home'),
re_path('^item/(?P<pk>[0-9]+)/$', views.ItemDetail.as_view(), name='item'),
path('^add/<int:id>', views.add_to_cart1, name="add_to_cart1"),

这是我的观点。py

def add_to_cart1(request, id):
item = get_object_or_404(Item, id=id)
order_item, created = CartItem.objects.get_or_create(
item=item,
user=request.user,
)
order_qs = CartItem.objects.filter(user=request.user, ordered=False)
if order_qs.exists():
order = order_qs[0]
if order.items.filter(item = item).exists():
order_item.quantity += 1
order_item.save()
messages.info(request, "Item qty was updated.")
return redirect("core:order-summary")
else:
order.items.add(order_item)
messages.info(request, "Item was added to your cart.")
return redirect("core:order-summary")
else:
order = CartItem.objects.create(
user=request.user,  )
order.items.add(order_item)
messages.info(request, "Item was added to your cart.")
return render(request,"index.html")

这就是我得到的错误:错误

在添加斜线"之后/"模板中的i.e. <a href="/add/<item-id>/">,获取错误:错误

这是item_detail.html

{% extends 'index.html' %}
{% load static %}
{% block content %}
<section class="w3l-specification-6">
<!-- /specification-6-->
<div class="specification-6-mian py-5">
<div class="container py-lg-5">
<div class="row story-6-grids text-left">
<div class="col-lg-5 story-gd">
<img src="{{object.image.url}}" class="img-fluid" alt="/">
<a href="/add/{{object.id}}/">
<button class="top_transmitv_cart" type="submit" name="submit" value="" >
Add to Cart
<span class="fa fa-shopping-cart"></span>
</a>                    </button>
</div>
<div class="col-lg-7 story-gd pl-lg-4">
<h3 class="hny-title"><span>{{object.title}}</span></h3>

<div class="row story-info-content mt-md-5 mt-4">
<div class="col-md-12 story-info">
<h5> <a href="#">Product Description</a></h5>
<p>{{object.description}}</p>
<br>
</div>
<div class="col-md-12 story-info">
<h5> <a href="#">Price</a></h5>
<span class="price"><del>&#x20b9;{{object.discount_price}}</del>&#x20b9;{{object.price}}</span>
<br>
</div>
</div>
<ul>
</ul>

</div>
</div>
</div>
</section>
{% endblock %}

这只是一个经常发生的小错误。无论你在哪里提供了在模板中的购物车中添加项目的url,只需在url后面加一个斜线,如下所示:-

<a href="/add/<item-id>/">

删除"从您的path。你不需要它,因为它不是正则表达式。(顺便说一下,正则表达式中的"^"表示"不",即不匹配(。所以你的path应该是:

path('add/<int:id>/', views.add_to_cart1, name="add_to_cart1"),

相关内容

  • 没有找到相关文章

最新更新