首页 > 解决方案 > NoReverseMatch at /checkout/ - Django 网站

问题描述

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

视图.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('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"))

标签: pythondjangodjango-modelsdjango-formsdjango-views

解决方案


结帐网址的 view.html 中缺少 Slug 字段


推荐阅读