I'm new in django. I try to delete element from database by ajax call. My ajax call send url with parameter which is pk of element to delete. Url looks fine but browser raise error that
url pattern doesn't match to any of urlpatterns in my url.py.
I have done similar project before and everything workerd good so I'm confused why it does't work now. Any Idea?
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')},
})
}
log:
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.
CodePudding user response:
You have typo error in your urls.py i.e. you have receptuta/delSkl/<int:id>/ in your urls.py and you are calling receptura/delSkl/<int:id>/
Change your urls.py from :
path('receptuta/delSkl/<int:id>/', views.delSkl, name='delSkl'),
To this:
path('receptura/delSkl/<int:id>/', views.delSkl, name='delSkl'),
