Modifications entre les versions 1 et 5 (s'étendant sur 4 versions)
Version 1 à la date du 2012-02-24 20:54:11
Taille: 5506
Éditeur: DavinBaragiotta
Commentaire: ébauche avec contenu frontend
Version 5 à la date du 2012-02-24 21:34:26
Taille: 8216
Éditeur: DavinBaragiotta
Commentaire:
Texte supprimé. Texte ajouté.
Ligne 1: Ligne 1:
= Django : Architecture d'un projet, Backend (admin) et Frontend (interfaces publiques d'un site) = = Django : Architecture d'un projet, Backend et Frontend =
Ligne 9: Ligne 9:
 * 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
Ligne 11: Ligne 28:
== PARTIE 1 : DÉVELOPPEMENT WEB AVEC DJANGO : ARCHITECTURE D'UN PROJET ==

=== 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)

=== 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)

=== 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.

=== Projet : modélisation ===

 * Joueur
 * Equipe
 * Match

----
Ligne 12: Ligne 67:

=== Création du projet ===

{{{
django-admin.py startproject liguemp
cd liguemp
}}}

 * survol des fichiers générés

{{{
python manage.py runserver
}}}

 * settings.py
  * DATABASE_*

=== Création d'une application ===

{{{
python manage.py startapp recrutement
}}}

 * 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'

{{{
python manage.py syncdb
}}}

=== 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
{{{def __unicode__(self):}}}
  * Joueur.prenom

 * delete liguemp.db

{{{
python manage.py syncdb
}}}

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

 * recrutement/models.py
  * Joueur.equipe

----

== PARTIE 3 : HANDS-ON : PROJET ET APPLICATIONS ==
Ligne 104: Ligne 243:
== PARTIE 3 : PROJET CARTO PIMPÉ == == PARTIE 4 : PROJET CARTO PIMPÉ ==
Ligne 211: Ligne 350:
== PARTIE 4 : HANDS-ON : CRÉER L'APPLICATION PROJET POUR LE PROJET CARTO == == PARTIE 5 : HANDS-ON : CRÉER L'APPLICATION PROJET POUR LE PROJET CARTO ==
Ligne 256: Ligne 395:



== CONCLUSION ==

 * documentation + interactivité + introspection
 * scripts + modules
 * enjoy!

Django : Architecture d'un projet, Backend et Frontend


INTRODUCTION

  • 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
    • 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


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

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)

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)

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.

Projet : modélisation

  • Joueur
  • Equipe
  • Match


PARTIE 2 : HANDS-ON : PROJET ET APPLICATIONS

Création du projet

django-admin.py startproject liguemp
cd liguemp
  • survol des fichiers générés

python manage.py runserver
  • settings.py
    • DATABASE_*

Création d'une application

python manage.py startapp recrutement
  • 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'

python manage.py syncdb

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/

def __unicode__(self):

  • Joueur.prenom
  • delete liguemp.db

python manage.py syncdb


PARTIE 3 : HANDS-ON : PROJET ET APPLICATIONS

Repartir des sources initiales

Repartir du projet créé lors de l'atelier du 2011-12-07 : sources initiales 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 4 : PROJET CARTO PIMPÉ

Télécharger les sources finales du projet pimpé : sources finales 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<path>.*)$', '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

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 5 : 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

Ateliers/2012-02-25/Plan (dernière édition le 2012-02-26 10:19:03 par DavinBaragiotta)