繁体   English   中英

无反向匹配 - django.urls.exceptions.NoReverseMatch

[英]No Reverse Match - django.urls.exceptions.NoReverseMatch

django.urls.exceptions.NoReverseMatch:未找到 arguments '('',)' 的“update_cart”反向。 尝试了 1 种模式:['cart/(?P[^/]+)$'] [18/Apr/2020 14:05:02] "GET /checkout/ HTTP/1.1" 500 157543 <---这是我在结帐页面上尝试 go 时收到的错误消息。

查看.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)

网址.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

错误由reverse()抛出

  1. 它接受 viewname 作为参数。
  2. 可以从定义的 urlpatterns 中找到视图名称。

我看到 urlpatterns 中没有定义视图fuisce-home 这就是错误来的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM