Send the ID of the record that I just created to another template

0

Good afternoon I'm doing an application in Django 1.10 and I need that when creating a record this sends me to another view with another template the Id of this record that I just created.

views.py

def datosBasicos(request): #En esta vista creo el nuevo registro
    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): #Necesito que me llegue a esta vista el id del registro creado
    beneficiario = Beneficiario.objects.get(id = id)
    return render(request,'beneficiario_create.html')

urls.py

   urlpatterns = [
    url(r'^$', beneficiario, name='beneficiario'),
    url(r'^beneficiario_create/(?P<id>\d+)/$', beneficiario_create,    name='beneficiario_create'),    
]
    
asked by jhon1946 15.01.2017 в 20:38
source

1 answer

1

Good afternoon so, if you want to render the data of the newly created object in the template, I do not see it necessary to send it to a new view.

Simply render the template once at the time of executing the action, instead of placing the return HttpResponseRedirect

finalize your view with a

render_to_response("beneficiario_create.html", locals(), context_instance = RequestContext(request))

If you want to have another view where you can manage and visualize the object details you can call beneficiary details, where if you receive the id

would be something like:

def datosBasicos(request, id = None): #En esta vista creo el nuevo registro
    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())
        render_to_response("beneficiario_create.html", locals(), context_instance = RequestContext(request))
    elif request.method == "GET":
        # aca deberías tener un blque para considerar que hacer en caso de que la peticion sea get, asumo debe venir el ID en la url y cargar la informacion
        id = request.GET["id"]
        beneficiario = Beneficiario.objects.get(id = id)
        return render(request,'datosBasicos.html', locals(), context_instance = RequestContext(request))

# Este view esta demas
# def beneficiario_create(request, id): #Necesito que me llegue a esta vista el id del registro # creado
#     beneficiario = Beneficiario.objects.get(id = id)
#     return render(request,'beneficiario_create.html')

Greetings, and I hope my answer will help you

    
answered by 16.01.2017 в 22:20