python - 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"))
解决方案
结帐网址的 view.html 中缺少 Slug 字段
推荐阅读
- swiftui - SwiftUI + 自动填充凭据提供程序
- r - 在 R 中,绘制每年唯一事件的平均数量
- java - 注册页面上primefaces命令按钮ajax失败后的Spring Security 302重定向
- sql - 将日期格式解析为雪花中的时间戳
- docker - docker 容器中信号处理程序的输出未从 docker run -t output 打印
- android - 单击不同的 RecyclerView 项目时停止视频
- eclipse - 如何使用颜色语法突出显示在 Spring Tool Suite/Eclipse 中显示百里香模板的注释部分?
- cpu - 如何触发CPU突发操作?
- project-organization - 如何更改 Azure DevOps 上的组织所有者?
- docker - 来自守护程序的 Docker 错误响应:处理 tar 文件时出错(退出状态 1):设备上没有剩余空间