I'm asking How I can call a variable with Python 2.7
and Django 1.10
which is defined in the function1 into the function2.
I defined a function1
like that :
def Test(request) :
form = TestForm(request.POST or None)
if form.is_valid() :
instance = form.save(commit=False)
return HttpResponseRedirect('toto')
context = {
"form" : form,
}
return render(request, 'form_Test.html', context)
And I would like to call the variable instance
inside my function2
:
def Test2(request) :
identity = instance.lastname
context = {
"identity" : identity,
}
return render(request, 'test2_identity.html',context)
I would like to know How it's possible to do this handle ?
----------------------------------------------------------------------------------------------
EDIT :
I tried to handle all things in the same view, with a preview step :
#views.py
def IdentityFormulary(request) :
form = IdentityForm(request.POST or None)
template_name = 'form_Identity.html'
if form.is_valid() :
if '_preview' in request.POST :
post = form.save(commit=False)
template_name = 'preview.html'
#if .is_valid() :
#post=form.save()
elif '_save' in request.POST :
post = form.save()
return HttpResponseRedirect('formulaire_traite')
context = {
"form" : form,
}
return render(request, template_name, context)
def CompletedFormulary(request) :
identity = Identity.objects.all().order_by("-id")[0]
context = {
"identity" : identity,
}
return render(request, 'recapitulatif_identity.html',context)
The preview.html file :
<!--preview.html-->
<h2 align="center"> Prévisualisation du formulaire </align> </h2>
{% block content %}
<h3> Récapitulatif des données enregistrées : </h3>
<li> Civilité : {{form.title}}</li>
<li> Nom : {{form.lastname}}</li>
<li> Prénom : {{form.firstname}}</li>
<li> Sexe : {{form.sex}}</li>
<li> Date de Naissance : {{form.birthday}}</li>
<li> Ville de Naissance : {{form.birthcity}}</li>
<li> Pays de Naissance : {{form.birthcountry}}</li>
<li> Nationalité : {{form.nationality}}</li>
<li> Profession : {{form.job}}</li>
<li> Adresse : {{form.adress}}</li>
<li> Ville : {{form.city}}</li>
<li> Code Postal : {{form.zip}}</li>
<li> Pays : {{form.country}}</li>
<li> Email : {{form.mail}}</li>
<li> Téléphone : {{form.phone}}</li>
{% endblock %}
<br></br>
<input type ="submit" name="_save" value="Valider la fiche individuelle" />
I 'm just blocking with the submit button in my preview.html because for the moment, it doesn't work