commit b8e8772c1b9262bfdd3f416022f10c0da97306f5 Author: Klafyvel Date: Sat Apr 7 15:49:12 2018 +0200 Ajout des fichiers diff --git a/creation_projet.png b/creation_projet.png new file mode 100644 index 0000000..4a77398 Binary files /dev/null and b/creation_projet.png differ diff --git a/exemple/mon_site/blog/__init__.py b/exemple/mon_site/blog/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/exemple/mon_site/blog/__pycache__/__init__.cpython-36.pyc b/exemple/mon_site/blog/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..1e95ef8 Binary files /dev/null and b/exemple/mon_site/blog/__pycache__/__init__.cpython-36.pyc differ diff --git a/exemple/mon_site/blog/__pycache__/admin.cpython-36.pyc b/exemple/mon_site/blog/__pycache__/admin.cpython-36.pyc new file mode 100644 index 0000000..6955d48 Binary files /dev/null and b/exemple/mon_site/blog/__pycache__/admin.cpython-36.pyc differ diff --git a/exemple/mon_site/blog/__pycache__/models.cpython-36.pyc b/exemple/mon_site/blog/__pycache__/models.cpython-36.pyc new file mode 100644 index 0000000..7823114 Binary files /dev/null and b/exemple/mon_site/blog/__pycache__/models.cpython-36.pyc differ diff --git a/exemple/mon_site/blog/__pycache__/urls.cpython-36.pyc b/exemple/mon_site/blog/__pycache__/urls.cpython-36.pyc new file mode 100644 index 0000000..faa8926 Binary files /dev/null and b/exemple/mon_site/blog/__pycache__/urls.cpython-36.pyc differ diff --git a/exemple/mon_site/blog/__pycache__/views.cpython-36.pyc b/exemple/mon_site/blog/__pycache__/views.cpython-36.pyc new file mode 100644 index 0000000..253f4d2 Binary files /dev/null and b/exemple/mon_site/blog/__pycache__/views.cpython-36.pyc differ diff --git a/exemple/mon_site/blog/admin.py b/exemple/mon_site/blog/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/exemple/mon_site/blog/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/exemple/mon_site/blog/apps.py b/exemple/mon_site/blog/apps.py new file mode 100644 index 0000000..7930587 --- /dev/null +++ b/exemple/mon_site/blog/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class BlogConfig(AppConfig): + name = 'blog' diff --git a/exemple/mon_site/blog/migrations/0001_initial.py b/exemple/mon_site/blog/migrations/0001_initial.py new file mode 100644 index 0000000..c4be2f0 --- /dev/null +++ b/exemple/mon_site/blog/migrations/0001_initial.py @@ -0,0 +1,23 @@ +# Generated by Django 2.0.4 on 2018-04-07 12:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Article', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('text', models.TextField(verbose_name='Texte')), + ('title', models.CharField(max_length=255, verbose_name='Titre')), + ('date', models.DateField(verbose_name='Date de parution')), + ], + ), + ] diff --git a/exemple/mon_site/blog/migrations/__init__.py b/exemple/mon_site/blog/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/exemple/mon_site/blog/migrations/__pycache__/0001_initial.cpython-36.pyc b/exemple/mon_site/blog/migrations/__pycache__/0001_initial.cpython-36.pyc new file mode 100644 index 0000000..6b0aab1 Binary files /dev/null and b/exemple/mon_site/blog/migrations/__pycache__/0001_initial.cpython-36.pyc differ diff --git a/exemple/mon_site/blog/migrations/__pycache__/__init__.cpython-36.pyc b/exemple/mon_site/blog/migrations/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..3b4ca14 Binary files /dev/null and b/exemple/mon_site/blog/migrations/__pycache__/__init__.cpython-36.pyc differ diff --git a/exemple/mon_site/blog/models.py b/exemple/mon_site/blog/models.py new file mode 100644 index 0000000..6d4853b --- /dev/null +++ b/exemple/mon_site/blog/models.py @@ -0,0 +1,19 @@ +from django.db import models + + +class Article(models.Model): + """Un article sur mon super site.""" + text = models.TextField(verbose_name="Texte") + title = models.CharField( + max_length=255, + verbose_name="Titre" + ) + date = models.DateField( + verbose_name="Date de parution" + ) + + def __str__(self): + return "'{}' : {}".format( + self.title, + self.date + ) diff --git a/exemple/mon_site/blog/tests.py b/exemple/mon_site/blog/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/exemple/mon_site/blog/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/exemple/mon_site/blog/urls.py b/exemple/mon_site/blog/urls.py new file mode 100644 index 0000000..af127c5 --- /dev/null +++ b/exemple/mon_site/blog/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from . import views + +app_name = "blog" +urlpatterns = [ + path('', views.index), +] diff --git a/exemple/mon_site/blog/views.py b/exemple/mon_site/blog/views.py new file mode 100644 index 0000000..fdf8bab --- /dev/null +++ b/exemple/mon_site/blog/views.py @@ -0,0 +1,14 @@ +from django.shortcuts import render +from django.http import HttpResponse + +from .models import Article + +def index(request): + articles = Article.objects.order_by('-date') + s = ("Bonjour et bienvenue" + " sur mon super site trop cool" + "\nMes articles :" + ) + for a in articles: + s += a.title + "\n" + return HttpResponse(s) diff --git a/exemple/mon_site/db.sqlite3 b/exemple/mon_site/db.sqlite3 new file mode 100644 index 0000000..8b405c0 Binary files /dev/null and b/exemple/mon_site/db.sqlite3 differ diff --git a/exemple/mon_site/manage.py b/exemple/mon_site/manage.py new file mode 100755 index 0000000..4734502 --- /dev/null +++ b/exemple/mon_site/manage.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mon_site.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) diff --git a/exemple/mon_site/mon_site/__init__.py b/exemple/mon_site/mon_site/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/exemple/mon_site/mon_site/__pycache__/__init__.cpython-36.pyc b/exemple/mon_site/mon_site/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..b2588d8 Binary files /dev/null and b/exemple/mon_site/mon_site/__pycache__/__init__.cpython-36.pyc differ diff --git a/exemple/mon_site/mon_site/__pycache__/settings.cpython-36.pyc b/exemple/mon_site/mon_site/__pycache__/settings.cpython-36.pyc new file mode 100644 index 0000000..48d8f45 Binary files /dev/null and b/exemple/mon_site/mon_site/__pycache__/settings.cpython-36.pyc differ diff --git a/exemple/mon_site/mon_site/__pycache__/urls.cpython-36.pyc b/exemple/mon_site/mon_site/__pycache__/urls.cpython-36.pyc new file mode 100644 index 0000000..8bd8c4a Binary files /dev/null and b/exemple/mon_site/mon_site/__pycache__/urls.cpython-36.pyc differ diff --git a/exemple/mon_site/mon_site/__pycache__/wsgi.cpython-36.pyc b/exemple/mon_site/mon_site/__pycache__/wsgi.cpython-36.pyc new file mode 100644 index 0000000..aeea1d5 Binary files /dev/null and b/exemple/mon_site/mon_site/__pycache__/wsgi.cpython-36.pyc differ diff --git a/exemple/mon_site/mon_site/settings.py b/exemple/mon_site/mon_site/settings.py new file mode 100644 index 0000000..e91b735 --- /dev/null +++ b/exemple/mon_site/mon_site/settings.py @@ -0,0 +1,121 @@ +""" +Django settings for mon_site project. + +Generated by 'django-admin startproject' using Django 2.0.4. + +For more information on this file, see +https://docs.djangoproject.com/en/2.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/2.0/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = ')x)iev^3&2wj32+6+l)g4&4&(!71q_xk_8f4&pjr&g$qmww2-1' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'blog' +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'mon_site.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'mon_site.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/2.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/2.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/2.0/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/exemple/mon_site/mon_site/urls.py b/exemple/mon_site/mon_site/urls.py new file mode 100644 index 0000000..50ac1f0 --- /dev/null +++ b/exemple/mon_site/mon_site/urls.py @@ -0,0 +1,22 @@ +"""mon_site URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/2.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('blog.urls')), +] diff --git a/exemple/mon_site/mon_site/wsgi.py b/exemple/mon_site/mon_site/wsgi.py new file mode 100644 index 0000000..9736331 --- /dev/null +++ b/exemple/mon_site/mon_site/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for mon_site project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mon_site.settings") + +application = get_wsgi_application() diff --git a/formation.md b/formation.md new file mode 100644 index 0000000..3b452f6 --- /dev/null +++ b/formation.md @@ -0,0 +1,374 @@ + +![](rezo.png) + +---- + +# ![](logo-django.png) + +# Une formation par Klafyvel et Nanoy2 + +---- + +# Qu'est-ce que Django peut faire ? +---- + +# Qu'est-ce que Django peut faire ? +- coope.rez +- Re2o +- Le site de la NASA +- Blogs +- ... + +---- + +# Qu'est-ce que Django ne peut pas faire ? + +--- +# Qu'est-ce que Django ne peut pas faire ? + +- Rien + +--- + +![](http://i0.kym-cdn.com/entries/icons/original/000/000/091/TrollFace.jpg) + +--- + +# Généralités sur Python : PIP +Installation : +```bash +sudo apt install python3-pip +``` +Utilisation : +```bash +pip3 install truc # installe truc +pip3 uninstall machin # vire truc +pip3 freeze > requirements.txt # Sauvegarde les packages +# installés +pip3 install -r requirements.txt # Installe les packages +# listés dans requirements.txt +``` + +---- + +# Généralités sur Python : VirtualEnv +##### (ou comment ne pas polluer son PC) +Installation : +```bash +pip3 install virtualenv +``` +Utilisation : +```bash +virtualenv env_formation +source env_formation/bin/activate +``` + +--- +# Généralités sur Python : VirtualEnvWrapper +###### (réservé aux gens supérieurs sous linux) +Installation : +```bash +pip install --user virtualenvwrapper +``` +Dans votre `.bashrc` +```bash +export WORKON_HOME=~/.virtualenvs +mkdir -p $WORKON_HOME +source ~/.local/bin/virtualenvwrapper.sh +``` +Utilisation : +```bash +mkvirtualenv monprojet +workon monprojet +rmvirtualenv monprojet +``` + +--- + +# Mon premier site : Un blog + +- Écrire des articles +- Lire des articles + +---- + +![](http://i.dailymail.co.uk/i/pix/2016/03/18/15/324D202500000578-3498922-image-a-33_1458315465874.jpg) + +--- + +# Comment démarrer un projet ? +Virtualenv : +```bash +cd là/où/vous/mettez/vos/projets/ +virtualenv env_formation +source env_formation/bin/activate +``` +VirtualenvWrapper : +```bash +mkvirtualenv env_formation +``` +Création du projet : +```bash +pip install django +django-admin startproject mon_site +cd blog +./manage.py migrate +./manage.py runserver +``` +--- + +![](creation_projet.png) + +--- +# Comment démarrer un projet ? +Création de l'application : +```bash +./manage.py startapp blog +``` +Enregistrement de l'application ( dans `mon_site/settings.py` ) : +```python +... +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'blog' +] +... +``` +--- +```bash +(env_formation) klafyvel@batman > ~/mon_site > tree +. +├── blog +│   ├── admin.py +│   ├── apps.py +│   ├── __init__.py +│   ├── migrations +│   │   └── __init__.py +│   ├── models.py +│   ├── tests.py +│   └── views.py +├── db.sqlite3 +├── manage.py +└── mon_site + ├── __init__.py + ├── __pycache__ + │   ├── __init__.cpython-36.pyc + │   ├── settings.cpython-36.pyc + │   └── urls.cpython-36.pyc + ├── settings.py + ├── urls.py + └── wsgi.py + +``` + +--- + +# L'architecture MVT + +Models Views Templates + +--- + +## M comme Model +Les imports + +```python +from django.db import models +``` + +--- + +## M comme Models + +```python +class Article(models.Model): + """Un article sur mon super site.""" + text = models.TextField(verbose_name="Texte") + title = models.CharField( + max_length=255, + verbose_name="Titre" + ) + date = models.DateField( + verbose_name="Date de parution" + ) + + def __str__(self): + return "'{}' : {}".format( + self.title, + self.date + ) + +``` + +--- +## Modifier la base de données + +```bash +./manage.py makemigrations blog +./manage.py migrate +``` +--- +## Time to play ! + +```python +./manage.py shell +>>> from blog.models import Article +>>> a = Article() +>>> a + +>>> from django.utils import timezone +>>> a.date = timezone.now() +>>> a.title = "Un super titre" +>>> a.text = "Un contenu vraiment très intéressant !" +>>> a + +>>> a.save() +``` +--- +## Time to play ! +```python +>>> b = Article() +>>> b.title = "Un autre article" +>>> b.date = timezone.now() +>>> b.text = "Du contenu" +>>> b.save() +>>> Article.objects.all() +, + ]> +``` +```python +>>> Article.objects.get(pk=1) + +>>> Article.objects.order_by('date') +, + ]> +``` +--- +## Time to play ! +```python +>>> import datetime +>>> d = datetime.timedelta(days=1) +>>> b.date += d +>>> b.save() +>>> Article.objects.filter(date__lte=timezone.now()) +]> + +``` + +--- + +# Mais quand est-ce qu'on affiche quelque chose dans le navigateur ? + +![](https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Fimg.ifcdn.com%2Fimages%2F48d342e94e28df0bb7c4e4817720d2c52f78c164d2c948d9c458c56cf5f29a2f_1.jpg&f=1) + +--- + +# L'architecture MVT +## V comme Views +```python +from django.shortcuts import render +from django.http import HttpResponse + +def index(request): + s = ("Bonjour et bienvenue" + "sur mon super site trop cool") + return HttpResponse(s) +``` + +--- + +## Routons mes bons +`blog/urls.py` (à créer) : +```python +from django.urls import path +from . import views + +app_name = "blog" +urlpatterns = [ + path('', views.index), +] +``` +`mon_site/urls.py`: +```python +... +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('blog.urls')), +] +``` + +--- +## Lancer le serveur : +```bash +./manage.py runserver +``` +## Tadaaaa : + +![](vue_1.png) + +--- +## Afficher des données ! +```python +from django.shortcuts import render +from django.http import HttpResponse + +from .models import Article + +def index(request): + articles = Article.objects.order_by('-date') + s = ("Bonjour et bienvenue" + " sur mon super site trop cool" + "\nMes articles :" + ) + for a in articles: + s += a.title + "\n" + return HttpResponse(s) +``` +--- +## Afficher des données ! +### Votre site : +![](vue_2.png) +### Vous : +![](https://cdn.fbsbx.com/v/t59.2708-21/21126129_120490105344717_7889159769410240512_n.gif?_nc_cat=0&_nc_eui2=v1%3AAeGbtxWnBq4kAcMxvAJ6tjLPyP_TnV5UJhqdGy0terc4gpiOO9l1EU1ONTTvqmITW8rcMgdvlLQ6-v7zVX3hqg8bUChi4SXCkI0nSQSRrXELBg&oh=4a1c25d3363f569467b446befbd3c98a&oe=5ACAEEFA) + +--- +# L'architecture MVT +## T comme Templates +```html +

Liste des articles

+{% for article in articles %} +
+

{{article.title}}

+

Article écrit le {{article.date}}

+{% endfor %} +``` +--- + +# Sites intéressants +- [Le blog Sam et Max](http://sametmax.com) + - [Article sur virtualenv](http://sametmax.com/les-environnement-virtuels-python-virtualenv-et-virtualenvwrapper/) + - [Article sur Pip](http://sametmax.com/votre-python-aime-les-pip/) + - [Un autre article pour comprendre à quel point l'écosystème Python c'est le feu](http://sametmax.com/creer-un-setup-py-et-mettre-sa-bibliotheque-python-en-ligne-sur-pypi/) +- [La doc de Django](http://docs.djangoproject.com/) +- [Zeste de Savoir](https://zestedesavoir.com/) +- [Djangogirl](https://tutorial.djangogirls.org/fr/) + +--- + +# Demander de l'aide : +- Vos Rézo(wo)mens :heart: + - IRC + - Telegram + - Mail + - Facebook + +- Forums + - [Zeste de Savoir](https://zestedesavoir.com/) + - [Stack Overflow](http://stackoverflow.com/) diff --git a/formation.pdf b/formation.pdf new file mode 100644 index 0000000..5a0a5c3 Binary files /dev/null and b/formation.pdf differ diff --git a/logo-django.png b/logo-django.png new file mode 100644 index 0000000..1dd8e9d Binary files /dev/null and b/logo-django.png differ diff --git a/logo-django.svg b/logo-django.svg new file mode 100644 index 0000000..75782c0 --- /dev/null +++ b/logo-django.svg @@ -0,0 +1,11 @@ + + + logo-django + Created with Sketch (http://www.bohemiancoding.com/sketch) + + + + + + + \ No newline at end of file diff --git a/rezo.png b/rezo.png new file mode 100644 index 0000000..0e1b3de Binary files /dev/null and b/rezo.png differ diff --git a/vue_1.png b/vue_1.png new file mode 100644 index 0000000..12513b3 Binary files /dev/null and b/vue_1.png differ diff --git a/vue_2.png b/vue_2.png new file mode 100644 index 0000000..c3f4707 Binary files /dev/null and b/vue_2.png differ