Good evening I'm doing an application with django and I need that after creating a record I address with HttpresponseRedirect taking the id of this new record to a new view and a different template.
url.py
urlpatterns = [
url(r'^$', beneficiario, name='beneficiario'),
url(r'^beneficiario_create/(?P<id>\d+)/$', beneficiario_create, name='beneficiario_create'),
]
wiews.py
def datosBasicos(request):
if request.method == 'POST':
beneficiario = Beneficiario()
beneficiario.numeroDocumento = request.POST['numeroDocumento']
beneficiario.nombreUno = request.POST['nombreUno']
beneficiario.save()
ben = Beneficiario.objects.get(id=beneficiario.id)
messages.success(request, validator.getMessage())
return HttpResponseRedirect('/beneficiario/beneficiario_create/%d/'%ben.id)
return render(request,'datosBasicos.html', informacion)
def beneficiario_create(request, id):
beneficiario = Beneficiario.objects.get(id = id)
return render(request,'beneficiario_create.html')
You don't have to (and shouldn't really) use a raw URL in HttpResponseRedirect
, you can you use reverse()
which allows you to include URL kwargs. For example:
return HttpResponseRedirect(reverse('beneficiario_create', kwargs={'id': ben.id))
To combine the reverse
with the HttpResponseRedirect
, you can use the django shortcut method redirect
like this:
from django.shortcuts import redirect
return redirect('beneficiario_create', id=ben.id)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.