class: center, middle  ## [Arthur Vuillard](mailto:arthur@hashbang.fr) Grenoble Meetup 27/11/2014 --- # Framework web ? > Un environnement de développement logiciel qui fournit des outils, des méthodes et des conventions pour créer une interface web dynamique --- # Django, framework web Approche "All batteries included" - ORM - moteur de template - gestion des requêtes HTTP --- # Django, framework web Approche "All batteries included" - traductions - formulaires - admin - authentification - caching - logging - emails - flux - commentaires - pagination - et plus encore... --- # Django, framework web Batteries vendues séparèment : - APIs REST - bases de données non relationnelles - données géographiques - [apps](https://www.djangopackages.com/) --- # Django, framework web .center[Model View Controller] .center[*ou*] .center[Model Template View] --- # Django, framework web Gestion du cycle de vie - django-admin.py/manage.py - création d'un projet/app - gestion des migrations --- # Versions Django | Python | Support ------ | --------------- | ---------------------------- 1.7.1 | 2.7
3.2+ | sortie de 1.9 1.6.8 | 2.6.5+
3.2+ | sortie de 1.8 1.4.16 | 2.5+ | LTS
~3 ans
mars 2015 --- # Installation ```bash pacman -S python-django pacman -S python2-django apt-get install python-django yum install django pip install django ``` --- # Création d'un projet ```bash $ django-admin startproject mysite $ find mysite mysite mysite/mysite mysite/mysite/settings.py mysite/mysite/urls.py mysite/mysite/wsgi.py mysite/mysite/__init__.py mysite/manage.py ``` --- # Création d'une app ```bash $ ./manage.py startapp myapp $ find myapp/ myapp/ myapp/migrations myapp/migrations/__init__.py myapp/tests.py myapp/models.py myapp/admin.py myapp/__init__.py myapp/views.py ``` --- # Commandes pratiques ```bash # Initialisation de la base ./manage.py syncdb # Application de modification au # schéma de la base ./manage.py migrate # Lancement d'un shell ./manage.py shell ``` --- # Commandes pratiques ```bash # Lancement des tests ./manage.py test # Lancer un server de dév ./manage.py runserver ``` --- # Fichier de configuration - Work out of the box - Contient : - paramètres de connexion aux bases de données - applications installées - timezone, langues - logging --- # Fichier de configuration ```python # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ) ``` --- # Fichier de configuration ```python # Database DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'db.sqlite3', } } ``` --- # Fichier de configuration ```python # Internationalization LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) STATIC_URL = '/static/' ``` --- name: models # Modèles ```bash cat mysite/myapp/models ``` ```python from django.db import models class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): question = models.ForeignKey(Question) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) ``` --- # Interface d'administration  --- # Interface d'administration  --- # Interface d'administration  --- name: view1 # Création d'une vue ```bash cat mysite/urls.py ``` ```python from django.conf.urls import patterns, include, url from django.contrib import admin urlpatterns = patterns('', url(r'^$', 'mysite.views.home', name='home'), url(r'^admin/', include(admin.site.urls)), ) ``` --- name: view2 # Création d'une vue ```bash cat myapp/views.py ``` ```python from django.shortcuts import ( get_object_or_404, render ) from myapp.models import Question def detail(request, question_id): question = get_object_or_404( Question, pk=question_id ) return render( request, 'myapp/detail.html', {'question': question} ) ``` --- name: view3 # Création d'une vue ```bash vim myapp/templates/myapp/detail.html ``` ```html
{{ question.question_text }}
{% for choice in question.choice_set.all %}
{{ choice.choice_text }}
{% endfor %}
``` --- # Pourquoi l'utiliser - tout en un - très bien documenté - communauté énorme et active - Python 3 - apprentissage rapide --- # Choses génantes - Frustrations - ORM - moteur de template - Parfois un peu compliqué - authentification - interface d'admin --- # Conclusion Pour aller plus loin : - https://docs.djangoproject.com/fr/1.7/intro/tutorial01/ - http://www.django-fr.org/ # Des questions ? ## .center[[arthur@hashbang.fr](mailto:arthur@hashbang.fr)]