没有反向匹配 - django.urls.exceptions.NoReverseMatch
Posted
技术标签:
【中文标题】没有反向匹配 - django.urls.exceptions.NoReverseMatch【英文标题】:No Reverse Match - django.urls.exceptions.NoReverseMatch 【发布时间】:2020-08-01 07:08:20 【问题描述】:django.urls.exceptions.NoReverseMatch:“update_cart”的反向参数“(”,)“未找到。尝试了 1 种模式:['cart/(?P[^/]+)$'] [18/Apr/2020 14:05:02] "GET /checkout/ HTTP/1.1" 500 157543
view.html
% for item in cart.products.all %
<tr><td> item </td><td>item.price</td>
<td><a href='% url "update_cart" item.slug %'> Remove</a></td></tr>
% endfor %
</table>
<br/>
<a href='% url "checkout" %'>Checkout</a>
% endif %
</div>
</div>
% endblock content %
用于订单的views.py
from django.urls import reverse
from django.shortcuts import render, HttpResponseRedirect
# Create your views here.
from carts.models import Cart
def checkout(request):
try:
the_id = request.session['cart_id']
cart = Cart.objects.get(id=the_id)
except:
the_id = None
return HttpResponseRedirect(reverse("fuisce-home"))
context =
template = "fuisce/home.html"
return render(request, template, context)
urls.py
from django.urls import path
from . import views
from carts import views as cart_views
from orders import views as order_views
urlpatterns = [
path('', views.home, name='fuisce-home'),
path('subscription/', views.subscription, name='fuisce-subscription'),
path('oneoff/', views.oneoff, name='fuisce-oneoff'),
path('about/', views.about, name='fuisce-about'),
path('contact/', views.contact, name='fuisce-contact'),
path('cart/', cart_views.view, name='cart'),
path('cart/<slug>', cart_views.update_cart, name='update_cart'),
path('checkout/', order_views.checkout, name='checkout'),
]
当我将 HttpResponse 从 def checkout 下方移动到 cart = Cart.objects.get(id=the_id) 下方时,问题似乎出现了。 (下面附上的代码更改)。有谁知道如何让它接受这种变化?
def checkout(request):
***return HttpResponseRedirect(reverse("fuisce-home"))***
try:
the_id = request.session['cart_id']
cart = Cart.objects.get(id=the_id)
except:
the_id = None
def checkout(request):
try:
the_id = request.session['cart_id']
cart = Cart.objects.get(id=the_id)
except:
the_id = None
***return HttpResponseRedirect(reverse("fuisce-home"))***
购物车的views.py
def update_cart(request, slug):
request.session.set_expiry(120000)
try:
the_id = request.session['cart_id']
except:
new_cart = Cart()
new_cart.save()
request.session['cart_id'] = new_cart.id
the_id = new_cart.id
cart = Cart.objects.get(id=the_id)
try:
product = Product.objects.get(slug=slug)
except Product.DoesNotExist:
pass
except:
pass
if not product in cart.products.all():
cart.products.add(product)
else:
cart.products.remove(product)
new_total = 0.00
for item in cart.products.all():
new_total += float(item.price)
request.session['items_total'] = cart.products.count()
cart.total = new_total
cart.save()
return redirect('cart')
models.py - 产品
class Product(models.Model):
product_id = models.AutoField
product_name = models.CharField(max_length=50)
price = models.DecimalField(max_digits=10, decimal_places=2)
desc = models.TextField()
slug = models.SlugField(unique=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
active = models.BooleanField(default=True)
image = models.ImageField(upload_to='fuisce/images', default="")
def __str__(self):
return self.product_name
【问题讨论】:
【参考方案1】:错误是reverse()抛出的
-
它接受 viewname 作为参数。
可以从定义的 urlpatterns 中找到视图名称。
我看到 urlpatterns 中没有定义视图名称 fuisce-home
。这就是错误出现的原因。
【讨论】:
感谢您的回复。我的网址中确实有 fuisce-home,我只是把它删掉了,因为我认为我显示了太多代码。我已经编辑了主要帖子以现在包含它 感谢您的想法,但不幸的是,该解决方案也不适用于我。 --------- django.urls.exceptions.NoReverseMatch:“update_cart”的反向参数“(”,)“未找到。尝试了 1 种模式: ['cart\\/(?P以上是关于没有反向匹配 - django.urls.exceptions.NoReverseMatch的主要内容,如果未能解决你的问题,请参考以下文章
没有反向匹配 - django.urls.exceptions.NoReverseMatch