0đź‘Ť
This is How I resolved my problem thanks to @brunodesthuilliers :
My views.py file :
#-*- coding: utf-8 -*-
from django.shortcuts import render, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.template import loader
from .models import Identity, Country
from .forms import IdentityForm
def IdentityAccueil(request) :
template = loader.get_template('accueil_Identity.html')
return HttpResponse(template.render(request))
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'
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)
My preview.html file :
<h2 align="center"> Prévisualisation du formulaire </align> </h2>
<form method='POST' action=''> {% csrf_token %}
{% 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" />
</form>
2đź‘Ť
Short answer: what your asking for is not possible, period. Even a global variable wouldn’t work, since nothing garantees that both requests will be served by the same process (and even then there would be a lot of other unsolvable issues).
FWIW that’s actually one of the reasons why we use databases a lot in web development – so we can persist state between requests (where a single-user Desktop app could just keep state in memory for the session).
Now you mention in your comments that
I don’t want to search from database because I want to get a data resume (something like cache) before to validate it. In this way, I can control data form before submission
If that’s the reason you want to share instance
between two views then it’s just not the way – you have a form
, forms DO have validation (that’s actually the main reason for django.forms
– validating user-submitted data), and you can hook into the validation process at different point to perform any extra validation. IOW : there’s just not reason to try to share a variable between two views to validate a form.
EDIT: ok now we have the X part of the XY problem:
What I would like to get is : user are filling a form in a HTML page. Then, he gets a resume and 2 options : submit (with data saving in MySQL database) or modify data form. The second option lets to get one more time the filled form and he can modify one or several values, then submit to database
And the answer is : you don’t need two views to handle the case, you just have to track the current “step” (initial submission / preview / edit / final submission), which is not really rocket science – this is easily done with the request method (a GET is for an empty form, anything else is either asking for a preview, edit or final submission) and the submit buttons names (to find out which of the preview / edit / final submission action should be performed). You can get an unsaved instance (for preview) using form.save(commit=False)
.
- [Answered ]-What do you think about my Django model structure?
- [Answered ]-Django query to Sum the lengths of ArrayFields
- [Answered ]-How do I place django rest nested serializer's fields to serializer?