我是 Django 的新手。我嘗試通過 ajax 呼叫從資料庫中洗掉元素。我的 ajax 呼叫發送帶有引數的 url,該引數是pk要洗掉的元素。Url 看起來不錯,但瀏覽器引發錯誤,即 url 模式與我的url.py. 我以前做過類似的專案,一切都很好,所以我很困惑為什么它現在不起作用。任何的想法?
網址.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'),
]
視圖.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.
uj5u.com熱心網友回復:
您的輸入錯誤,urls.py即您的receptuta/delSkl/<int:id>/輸入錯誤,urls.py并且您正在呼叫receptura/delSkl/<int:id>/
改變你urls.py的:
path('receptuta/delSkl/<int:id>/', views.delSkl, name='delSkl'),
對此:
path('receptura/delSkl/<int:id>/', views.delSkl, name='delSkl'),
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/412657.html
標籤:
