首页 > 解决方案 > urls.py 中可用的 URL 在 Django 2 中仍然得到 404

问题描述

我写了一个函数views.py如下

def removeinvoices(request,invoiceID,creatorID,deletorid,typeofchange):
    form = invoicestorageForm()
    form.invoiceid = invoiceID
    form.creatorid = creatorID
    form.deletorid = deletorid
    form.typeofchange = typeofchange
    form.save()
    thatInvoice = taxInvoice.objects.filter(invoiceid=invoiceID).first()
    thatInvoice.delete()
    messages.success(request, "Invoice ID "+ invoiceID +" removed Successfully !")
    return redirect("vieweinvoices")

并写urls.py如下:

path("removeinvoices/<int:invoiceID>/<int:creatorID/><int:deletorid>/<str:typeofchange>", views.removeinvoices, name="removeinvoices",)

而且我在JS中动态生成href,我能够正确地获取所有变量,但是当我点击href时 仍然说404错误图片

我可以很好地处理图片中提到的所有其他 URL。

不知道我哪里错了!

标签: pythondjangodjango-urls

解决方案


要使用strPath您可以使用re_path()

urls.py 使用re_path()

re_path(r"^removeinvoices/(?P<invoiceID>\d+)/(?P<creatorID>\d+)/(?P<deletorid>\d+)/(?P<typeofchange>\w+)/$", views.removeinvoices, name="removeinvoices",)

网址: http: //127.0.0.1 :9091/5/1/1/delete/

此外,在您的网址中,您/<int:creatorID/><int:deletorid>.

改变:

path("removeinvoices/<int:invoiceID>/<int:creatorID/><int:deletorid>/<str:typeofchange>", views.removeinvoices, name="removeinvoices",)

至:

path("removeinvoices/<int:invoiceID>/<int:creatorID>/<int:deletorid>/<str:typeofchange>", views.removeinvoices, name="removeinvoices",)

推荐阅读