h1. Plan détaillé

h2. INTRODUCTION

* Présentations ** Davin Baragiotta ** Montréal-Python (MP, sprints/hackathons, ateliers, PyCon) * Objectifs ** connaître l'architecture d'un projet : principaux fichiers ** gérer data dans backend ** présenter data dans frontend * Documentation ** doc : bonne version de django *** officielle : https://docs.djangoproject.com/en/1.3/ *** français : http://docs.django-fr.org/ ** tutoriel *** Part 1 : créer un projet, créer une app, jouer avec ORM (API) _couvert ici_ *** Part 2 : backend (admin) _couvert ici_ *** Part 3 : frontend _couvert ici, plus straightforward même ;)_ *** Part 4 : forms et vues génériques _pas couvert ici_ * Environnement technique ** Python >= 2.5 ** Django 1.3.1 *** Mac OS X : voir tutoriel Part 1 pour manip supplémentaire

h2. PARTIE 1 : DÉVELOPPEMENT WEB AVEC DJANGO : ARCHITECTURE D'UN PROJET

h3. Développement web

* schéma : le développement web ** rôle d'un framework web : aider à construire réponse à une requête * développement (DEV) * déploiement (PROD, TEST)

h3. Django

Pourquoi django vs autre framework * all-in-one * admin : meilleur vendeur ;) * docs * communauté : utilisation répandue

Principaux fichiers * models.py * admin.py * urls.py * views.py * templates (HTML)

h3. Projet : définition du besoin

Montréal-Python a une ligue de hockey cosom _(ce n'est pas vrai)_. On veut gérer quels joueurs sont dans quelles équipes... ... et planifier les matchs de la saison.

h3. Projet : modélisation

* Joueur * Equipe * Match

h2. PARTIE 2 : HANDS-ON : PROJET ET APPLICATIONS

h3. Création du projet

<pre> django-admin.py startproject liguemp cd liguemp </pre>

* survol des fichiers générés

<pre> python manage.py runserver </pre>

* settings.py ** DATABASE_*

h3. Création d'une application

<pre> python manage.py startapp recrutement </pre>

* survol fichiers

* recrutement/models.py ** documentation : https://docs.djangoproject.com/en/1.3/topics/db/models/ https://docs.djangoproject.com/en/1.3/ref/models/fields/ ** yeah! on code! *** Joueur.nom *** Equipe.nom

* settings.py ** INSTALLED_APP *** 'recrutement'

<pre> python manage.py syncdb </pre>

h3. Backend : gérer les données dans l'admin

* urls.py _activer admin : décommenter_ ** import ** autodiscover ** url : admin/

* http://127.0.0.1:8000/

* settings.py ** INSTALLED_APP *** 'django.contrib.admin'

* recrutement/admin.py _enregistrer modèles voir tutoriel Part 2_ https://docs.djangoproject.com/en/1.3/intro/tutorial02/

* http://127.0.0.1:8000/admin/

* recrutement/models.py <pre>def unicode(self):</pre> ** Joueur.prenom

* delete liguemp.db

<pre> python manage.py syncdb </pre>

> fixtures > https://docs.djangoproject.com/en/1.3/ref/django-admin/ > > south > http://south.aeracode.org/

* recrutement/models.py ** Joueur.equipe

h3. Frontend : présenter les données

* recrutement/urls.py https://docs.djangoproject.com/en/1.3/topics/http/urls/ ** home

* recrutement/views.py https://docs.djangoproject.com/en/1.3/topics/http/views/ _voir tutoriel Part 3 (version shortcut)_ https://docs.djangoproject.com/en/1.3/intro/tutorial03/

* templates https://docs.djangoproject.com/en/1.3/topics/templates/ ** créer répertoire templates ** settings.py <pre> os.path.join(os.path.dirname(file), "templates"), import os </pre> ** templates/recrutement/home.html

* recrutement/views.py _passer une variable au template_

* templates/recrutement/home.html _utiliser une variable :_ <pre> var </pre>

* recrutement/views.py _endroit où on code en logique en Python plus simple si explore interactivement..._

<pre> python manage.py shell </pre>

* ORM (API) : object relation mapping <pre> from recrutement.models import * equipes = Equipe.objects.all() for e in equipes: print e e = equipes[0] e.joueur_set.all() j = Joueur.objects.get(id=1) j.id j = Joueur.objects.get(id=314) e.id joueurs = Joueur.objects.filter(equipenomstartswith='Pyth') </pre>

* recrutement/models.py <pre> related_name = "joueurs" </pre>

* relancer shell <pre> e.joueurs.all() e.joueurs.count() </pre>

* recrutement/views.py _passer les variables pertinentes pour accueil_

* templates/recrutement/home.html _boucle for dans template :_ <pre> {% for e in equipes %} {% endfor %} </pre>

h2. PARTIE 3 : PROJET LIGUEMP PIMPÉ

Télécharger les sources du projet pimpé : http://montrealpython.org/r/attachments/8

Extraire (_sans clash de nom avec projet en cours_)

Pimpé? Quoi de neuf?

h3. Héritage de templates

* base.html <pre> {% block main %} </pre>

* templates <pre> {% extends "base.html" %} {% block main %} </pre>

h3. Fichiers statiques : CSS, images et js

https://docs.djangoproject.com/en/1.3/howto/static-files/

* répertoire : static ** css ** images ** js

* settings.py ** STATICFILES_DIRS <pre> os.path.join(os.path.dirname(file), "static"), </pre>

* urls.py <pre> from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns += staticfiles_urlpatterns() </pre>

h3. Connexion du user

* templates ** connexion.html ** deconnexion.html

* urls.py ** connexion ** deconnexion

* settings.py <pre> LOGIN_REDIRECT_URL = "/" </pre>

h3. URL avec paramètres

* pages de détail : capter l'id de l'objet ** urls.py : import des urls de l'app recrutement ** recrutement/urls.py ** recrutement/views.py ** templates/recrutement/*

h3. Refactoring du home

* urls home dans views.py à la racine

h3. Admin pimpé : ModelAdmin

* recrutement/admin.py

* classes JoueurAdmin, EquipeAdmin _héritent de ModelAdmin_ ** enregistrer Modele avec ModeleAdmin <pre> admin.site.register(Joueur, JoueurAdmin) </pre> ** config ModeleAdmin <pre> list_display search_fields list_filter </pre> ** plus? https://docs.djangoproject.com/en/1.3/ref/contrib/admin/ *** fields *** fieldsets

h3. Charger données initiales : fixtures

https://docs.djangoproject.com/en/1.3/ref/django-admin/

* créer répertoire fixtures à la racine

<pre> python manage.py dumpdata > initial_data.json </pre>

* mettre initial_data.json dans fixtures

* pour récupération data : <pre> python manage.py syncdb no (pas créer user) python manage.py loaddata fixtures/initial_data.json </pre>

h2. PARTIE 4 : HANDS-ON : CRÉER L'APPLICATION SAISON POUR LE PROJET LIGUEMP

h3. Ajouter modèle Match

* Match ** date ** lieu ** equipe1 ** equipe2 ** score1 ** score2

h3. Autre exercice : age d'un joueur

* Joueur.date_naissance * Joueur.age() _ajouter une methode age() sur la classe Joueur utilisant la date de naissance_

h2. CONCLUSION : POUR CONTINUER

* Autres aspects non couverts (quelques uns) ** permissions et décorateurs ** manage.py inspectdb ** manage.py test ** forms ** generic views ** templates : tags, filtres

* Mailing list Django users : http://groups.google.com/group/django-users

* Channel IRC #django : irc://irc.freenode.net/django

* Contribs, plugins https://docs.djangoproject.com/en/dev/ref/contrib/ ** south ** reversion ** Autre : pypi http://pypi.python.org/

* Enjoy!

h2. SONDAGE DE SATISFACTION

* http://www.surveymonkey.com/s/XKP2NP7