我在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)
然后我有一个函数通过从其他地方获取主键来插入带参数id的数据。
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})
如何重定向到detel_kar
视图?如果我在tambah_kel
函数
return redirect('detel_kar')
它将返回错误
Reverse for 'detel_kar' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
urls.py
from django.conf.urls import url
from . 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'),
]
答案 0 :(得分:1)
在urls.py
中,您已定义:
url(r'^karyawan/detel_kar/(?P<id>\d+)/$',views.detel_kar, name='detel_karyawan'),
即,调用函数detel_kar()
的视图名为'detel_karyawan'
。这是您需要在重定向中使用的名称:
return redirect('detel_karyawan')
但是视图需要ID,因此在调用时必须提供ID:
return redirect('detel_karyawan', id=id)
(根据您视图中的逻辑确定id
)。