= Atelier 2011-12-12 : Plan détaillé = == INTRODUCTION == * Objectifs * connaître l'architecture d'un projet : principaux fichiers * présenter des données dans le frontend (interface publique) avec Django * Documentation * doc : bonne version de django * officielle : https://docs.djangoproject.com/en/1.1/ * français : http://docs.django-fr.org/ * AUF : page wikiteki sur [[Django]] * tutoriel * Part 1 : créer un projet, créer une app, jouer avec ORM (API) ''couvert en partie lors de atelier 2011-12-07'' * Part 2 : backend (admin) ''couvert lors de atelier 2011-12-07'' * Part 3 : frontend ''couvert ici, plus efficacement même ;)'' * Part 4 : forms et vues génériques ''pas couvert ici'' * Environnement technique * Python >= 2.5 * Django 1.1.1 * Mac OS X : voir tutoriel Part 1 pour manip supplémentaire == PARTIE 1 : RAPPELS == === Django === Principaux fichiers * models.py * admin.py * urls.py * views.py * templates (HTML) === Projet carto : définition du besoin === L'AUF veut procéder à la cartographie de ses établissements membres. On veut savoir quelle formation est donnée où... ... et répertorier les projets des membres. === Projet carto : modélisation === * Etablissement * Formation * Projet == PARTIE 2 : HANDS-ON : PROJET ET APPLICATIONS == === Repartir des sources initiales === Repartir du projet créé lors de l'atelier du 2011-12-07 : sources initiales [[attachment:sources.tar.gz]] Nous avions alors : * Créé le projet carto * Créé l'application etablissements * Créé les modèles Etablissement et Formation * Géré dans l'admin des Etablissements et des Formations === Frontend : présenter les données === * urls.py https://docs.djangoproject.com/en/1.1/topics/http/urls/ * home * views.py https://docs.djangoproject.com/en/1.1/topics/http/views/ ''voir tutoriel Part 3 (version shortcut)'' https://docs.djangoproject.com/en/1.1/intro/tutorial03/ * templates https://docs.djangoproject.com/en/1.1/topics/templates/ * créer répertoire templates * settings.py {{{ import os TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. os.path.join(os.path.dirname(__file__), "templates"), ) }}} * templates/home.html * views.py ''passer une variable au template'' * templates/home.html ''utiliser une variable :'' {{{ {{ var }} }}} * views.py ''endroit où on code la logique en Python...'' ''...plus simple si explore interactivement'' {{{ python manage.py shell }}} * ORM (API) : object relationnal mapping {{{ from etablissements.models import * etablissements = Etablissement.objects.all() for e in etablissements: print e e = etablissements[0] e.formation_set.all() f = Formation.objects.get(id=1) f.id f = Formation.objects.get(id=314) e.id formations = Formation.objects.filter(etablissement__nom__contains='Hanoi') }}} * etablissements/models.py {{{ related_name = "formations" }}} * relancer shell {{{ e.formations.all() e.formations.count() }}} * views.py ''passer les variables pertinentes pour accueil'' * templates/home.html ''boucle for dans template :'' {{{ {% for e in etablissements %} {% endfor %} }}} == PARTIE 3 : PROJET CARTO PIMPÉ == Télécharger les sources finales du projet pimpé : sources finales [[attachment:sources.tar.gz]] Pimpé? Quoi de neuf? === Héritage de templates === * base.html {{{ {% block main %} }}} * templates {{{ {% extends "base.html" %} {% block main %} }}} === Fichiers statiques : CSS, images et js === https://docs.djangoproject.com/en/1.1/howto/static-files/ * répertoire : static * css * images * js * settings.py {{{ MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media') MEDIA_URL = '/static/' }}} * urls.py {{{ from django.conf import settings if settings.DEBUG: urlpatterns += patterns('', (r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), ) }}} === Connexion du user === * templates * connexion.html * deconnexion.html * urls.py * connexion * deconnexion * settings.py {{{ LOGIN_REDIRECT_URL = "/" }}} === URL avec paramètres === * pages de détail : capter l'id de l'objet * urls.py : import des urls de l'app etablissements * etablissements/urls.py * etablissements/views.py * templates/etablissements/* === Admin pimpé : ModelAdmin === * etablissements/admin.py * classes EtablissementAdmin, FormationAdmin ''héritent de ModelAdmin'' * enregistrer Modele avec ModeleAdmin {{{ admin.site.register(Formation, FormationAdmin) }}} * config ModeleAdmin {{{ list_display search_fields list_filter }}} * plus? https://docs.djangoproject.com/en/1.1/ref/contrib/admin/ * fields * fieldsets === Charger données initiales : fixtures === https://docs.djangoproject.com/en/1.1/ref/django-admin/ * créer répertoire fixtures à la racine {{{ python manage.py dumpdata > initial_data.json }}} * mettre initial_data.json dans fixtures * pour récupération data : {{{ python manage.py syncdb no (pas créer user) python manage.py loaddata fixtures/initial_data.json }}} == PARTIE 4 : HANDS-ON : CRÉER L'APPLICATION PROJET POUR LE PROJET CARTO == === Ajouter modèle Projet === * Projet * nom * date_debut * date_fin * etablissements === Autre exercice : durée d'un Projet et avancement d'un Projet === * Projet.duree() {{{ """Durée prévue en fonction des dates de début et de fin""" }}} * Projet.avancement() {{{ """Durée réelle à partir de la date de début jusqu'à maintenant.""" }}} == 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!