I'm really new with Django and I would like to get your minds, advices, ... in order to improve the first part of my project.
For the moment, I created a model form which is named Identity
and users can fill the form, get a preview with modification before to submit data.
As I said, I'm beginning to use Django and I'm sure that my script need modification, maybe an easier way to do what I want.
My forms.py file :
#-*- coding: utf-8 -*-
from django import forms
from BirthCertificate.models import *
class IdentityForm(forms.ModelForm) :
class Meta :
model = Identity
fields = '__all__'
My views.py file looks like :
#-*- 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)
def Consultation(request) :
identity = Identity.objects.all().order_by("-id")[:10] #Les 10 dernières fiches créées
identity_France = Identity.objects.filter(country='64').order_by("-id")[:10] #Les 10 dernières fiches où la personne habite en France
query = request.GET.get('q')
if query :
toto = Identity.objects.filter(lastname__icontains=query)
else :
toto = []
context = {
"identity" : identity,
"identity_France" : identity_France,
"query" : query,
"toto" : toto,
}
return render(request, 'resume.html', context)
My accueil_identity.html looks like :
<h2 align="center"> Gestion des fiches individuelles </align> </h2>
<p> Veuillez cliquer sur l'opération à effectuer : </p>
<p> </p>
<p>* <a href="http://localhost:8000/Identity/formulaire">Créer une nouvelle fiche individuelle</a></p>
<p> * <a href="http://localhost:8000/Identity/recherche">Consulter/Editer une fiche individuelle</a></p>
<p> * Supprimer une fiche individuelle </p>
My form_identity.html file looks like :
<!--DOCTYPE html -->
<html>
<body>
<h1 align="center"> Formulaire de fiche individuelle </h1>
<form method='POST' action=''> {% csrf_token %}
<h3> Partie contenant les informations de la fiche individuelle </h3>
{{ form.as_ul }}
{{ value|date:"%d/%m/%Y" }}
<br></br>
<input type ="submit" name="_save" value="Valider la fiche individuelle" />
<input type ="submit" name="_preview" value="Prévisualiser la fiche individuelle" />
</form>
</body>
</html>
My preview.html file as following :
<h2 align="center"> Prévisualisation de la fiche individuelle </align> </h2>
<form method='POST' action='/Identity/accueil'> {% 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>
And my recapitulative_identity.html file :
<h2 align="center"> Votre formulaire a été validé </align> </h2>
{% block content %}
Votre personne porte le numéro : {{ identity.id }}
<h3> Récapitulatif des données enregistrées : </h3>
<li> Civilité : {{identity.title}}</li>
<li> Nom : {{identity.lastname}}</li>
<li> Prénom : {{identity.firstname}}</li>
<li> Sexe : {{identity.sex}}</li>
<li> Date de Naissance : {{identity.birthday}}</li>
<li> Ville de Naissance : {{identity.birthcity}}</li>
<li> Pays de Naissance : {{identity.birthcountry}}</li>
<li> Nationalité : {{identity.nationality}}</li>
<li> Profession : {{identity.job}}</li>
<li> Adresse : {{identity.adress}}</li>
<li> Ville : {{identity.city}}</li>
<li> Code Postal : {{identity.zip}}</li>
<li> Pays : {{identity.country}}</li>
<li> Email : {{identity.mail}}</li>
<li> Téléphone : {{identity.phone}}</li>
<br></br>
{% endblock %}
<br></br>
<form method='POST' action='/Identity/accueil'> {% csrf_token %}
<input type ="submit" value="Retour fiche identité" />
</form>
<form method='POST' action='/BirthCertificate/accueil'> {% csrf_token %}
<input type ="submit" value="Création d'un acte de naissance" />
</form>
My resume.html file :
<h2 align="center"> Affichage de toutes les fiches individuelles </align> </h2>
<br></br>
{% block content %}
<h4> Récapitulatif des 10 dernières fiches individuelles créées: </h4>
<ul>
{% for item in identity %}
<li>{{ item }}</li>
{% endfor %}
</ul>
<h4> Récapitulatif des 10 dernières fiches individuelles créées habitant en France: </h4>
<ul>
{% for item in identity_France %}
<li>{{ item }}</li>
{% endfor %}
</ul>
<h4> Recherche par nom </h4>
<form method="GET" action="">
<input type="text" name="q" placeholder="Rechercher un nom" value="{{ request.GET.q }}">
<input type="submit" value="Rechercher">
</form>
<ul>
{% for item in toto %}
<li> {{ item }} </li>
{% endfor %}
</ul>
{% endblock %}
I need some advice, part of script in order to improve mine, ... :)