带有参数的Ajax url.当前路径与这些路径都不匹配



我是django的新手。我试图通过ajax调用从数据库中删除元素。我的ajax调用发送url,参数是要删除的元素的pk。Url看起来不错,但浏览器引发错误url模式与我的url.py中的任何url模式都不匹配。我以前做过类似的项目,一切都很好,所以我很困惑为什么现在不起作用。有什么想法吗?

urls.py:

urlpatterns = [
path('', views.home,name='home'),
path('mojerec',views.mojeRec,name='mojerec'),
path('dodajrec',views.dodajRec,name='dodajrec'),
path('receptura/(<int:receptura_id>)',views.receptura,name='receptura'),
path('formJson/<str:skl>/', views.formJson, name='formJson'),
path('receptura/formJson/<str:skl>/', views.formJson, name='formJson'),
path('receptura/dodajskl/<str:sklId>/', views.dodajsklJson, name='dodajsklJson'),
path('receptura/aktualizujTabela/<str:sklId>/', views.aktualizujTabela, name='aktualizujTabela'),
path('receptuta/delSkl/<int:id>/', views.delSkl, name='delSkl'),

]

views.py

def delSkl (request,id):
deletedElement=Skladnik.objects.filter(pk=id)
response=serializers.serialize("python", deletedElement)
deletedElement.delete()
print('response', response)
sys.stdout.flush()
return JsonResponse({'response':response})

myjs.js

function usuwanieSkladnika (pk){
$.ajax({
type: 'GET',
url: `delSkl/${ pk }/`,
success : function(response){console.log('sukces ajaxa z del');
cardBox.innerHTML=''
tabelaDocelowa.innerHTML='';
updateTable()
},//koniec sukcesa
error : function (error){console.log('brak sukcesu ajaxa z del')},
})
}

日志:

Page not found (404)
Request Method:     GET
Request URL:    http://localhost:8000/receptura/delSkl/13/
Using the URLconf defined in recipe.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
mojerec [name='mojerec']
dodajrec [name='dodajrec']
receptura/(<int:receptura_id>) [name='receptura']
formJson/<str:skl>/ [name='formJson']
receptura/formJson/<str:skl>/ [name='formJson']
receptura/dodajskl/<str:sklId>/ [name='dodajsklJson']
receptura/aktualizujTabela/<str:sklId>/ [name='aktualizujTabela']
receptuta/delSkl/<int:id>/ [name='delSkl']
users/
The current path, receptura/delSkl/13/, didn’t match any of these.

您的urls.py中有打字错误,即您的urls.py中有receptuta/delSkl/<int:id>/,并且您正在调用receptura/delSkl/<int:id>/

将您的urls.py从:更改

path('receptuta/delSkl/<int:id>/', views.delSkl, name='delSkl'),

对此:

path('receptura/delSkl/<int:id>/', views.delSkl, name='delSkl'),

最新更新