python - How to use redirect with parameter in Django? -
i have function in views.py:
def detel_kar(request, id): detel = {} detel['detels'] = dippegawai.objects.filter(pegid=id) detel['kels']=dipkeluargapeg.objects.filter(pegid_id=id) return render(request, 'karyawan/detel_kar.html', detel)
i have function insert data parameter id getting primary key elsewhere.
def tambah_kel(request, id): kar = dippegawai.objects.get(pegid=id) if request.method == "post": kel=dipkeluargapeg(pegid=kar) form = datakeluarga(request.post,instance=kel) if form.is_valid(): form.save(commit=true) return redirect('detel_kar') else: form = datakeluarga() return render(request, 'karyawan/tambah_kel.html', {'form': form, 'kars': kar})
how redirect detel_kar
view? if use code in tambah_kel
function
return redirect('detel_kar')
it return anerror
reverse 'detel_kar' arguments '()' , keyword arguments '{}' not found. 0 pattern(s) tried: []
urls.py
from django.conf.urls import url . import views urlpatterns=[ url(r'^$', views.index, name='index'), url(r'^tambah/$', views.tambah, name='tambah_kar'), url(r'^karyawan/detel_kar/(?p<id>\d+)/$',views.detel_kar, name='detel_karyawan'), url(r'^karyawan/edit_kar/(?p<id>\d+)/$',views.edit_kar, name='edit_karyawan'), url(r'^karyawan/del_kar/(?p<id>\d+)/$',views.del_kar, name='del_karyawan'), url(r'^karyawan/tambah_kel/(?p<id>\d+)/$',views.tambah_kel, name='tambah_keluarga'), ]
in urls.py
, have defined:
url(r'^karyawan/detel_kar/(?p<id>\d+)/$',views.detel_kar, name='detel_karyawan'),
i.e., view calls function detel_kar()
named 'detel_karyawan'
. name need use in redirect:
return redirect('detel_karyawan')
however view expects id, must supply id when calling it:
return redirect('detel_karyawan', id=id)
(where id
determined based on logic in view).