Présentation Création d’une page perso Introduction au framework Django Création d’une page perso Myriam BEGEL Cachan Réseau à Normale Sup’ Mardi 13 décembre 2016 Myriam BEGEL (Cr@ns) Introduction au framework Django Création d’une page perso 13/12/2016 1 / 28
28
Embed
Introduction au framework Django Création d'une page perso · Django est un framework de développement web en Python. The web framework for perfectionists with deadlines. La documentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Présentation Création d’une page perso
Introduction au framework DjangoCréation d’une page perso
Myriam BEGEL
Cachan Réseau à Normale Sup’
Mardi 13 décembre 2016
Myriam BEGEL (Cr@ns) Introduction au framework Django Création d’une page perso 13/12/2016 1 / 28
2 Création d’une page persoCréer un projetCréation d’un modèleDécouverte de l’interface d’administrationUtiliser un gabaritFormulaire de contactTester son code
Myriam BEGEL (Cr@ns) Introduction au framework Django Création d’une page perso 13/12/2016 2 / 28
2 Création d’une page persoCréer un projetCréation d’un modèleDécouverte de l’interface d’administrationUtiliser un gabaritFormulaire de contactTester son code
Myriam BEGEL (Cr@ns) Introduction au framework Django Création d’une page perso 13/12/2016 3 / 28
1 L’utilisateur envoie une requête2 Django appelle la Vue correspondant à l’URL3 La Vue récupère les données, le gabarit4 et génère un rendu HTML pour l’utilisateur
Myriam BEGEL (Cr@ns) Introduction au framework Django Création d’une page perso 13/12/2016 9 / 28
2 Création d’une page persoCréer un projetCréation d’un modèleDécouverte de l’interface d’administrationUtiliser un gabaritFormulaire de contactTester son code
Myriam BEGEL (Cr@ns) Introduction au framework Django Création d’une page perso 13/12/2016 10 / 28
Un modèle est une classe python qui représentera un objet dans labase de données.Les modèles étendent la classe models.ModelLes champs de l’objet que l’on veut enregistrer sont des extensions demodels.Field. Il y en a pour tous les goûts.2
2DocMyriam BEGEL (Cr@ns) Introduction au framework Django Création d’une page perso 13/12/2016 14 / 28
On peut décrire un formulaire par une classe avec des champsétendant forms.Form dans forms.pyComme pour les modèles, il y une flopée de forms.FieldOn peut aussi créer automatiquement un formulaire à partir d’unmodèle.
1 class ContactForm(ModelForm):2 class Meta:3 model = Contact4 fields = [’nom’, ’mail’, ’raison’, ’message’]
Myriam BEGEL (Cr@ns) Introduction au framework Django Création d’une page perso 13/12/2016 24 / 28
Ecrire le modèle correspondantOn pensera à actualiser urls.py, views.py et admin.py (et base.html)Les gabarits savent générer automatiquement le HTML pour unformulaire.
1 class ContactForm(ModelForm):2 class Meta:3 model = Contact4 fields = [’nom’, ’mail’, ’raison’, ’message’]
Attention à ne pas oublier la protection CSRF5
5DocMyriam BEGEL (Cr@ns) Introduction au framework Django Création d’une page perso 13/12/2016 25 / 28
On ajoute une méthode recent au modèle Publication et on la testedans tests.pyPour lancer les tests : ./manage.py test perso_ appDjango lancera les fonctions commençant par ’test’ dans les classesétendant ’TestCase’
Myriam BEGEL (Cr@ns) Introduction au framework Django Création d’une page perso 13/12/2016 28 / 28