Ajout des fichiers

This commit is contained in:
Klafyvel 2018-04-07 15:49:12 +02:00
commit b8e8772c1b
34 changed files with 633 additions and 0 deletions

BIN
creation_projet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

View file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class BlogConfig(AppConfig):
name = 'blog'

View file

@ -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')),
],
),
]

View file

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

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -0,0 +1,7 @@
from django.urls import path
from . import views
app_name = "blog"
urlpatterns = [
path('', views.index),
]

View file

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

BIN
exemple/mon_site/db.sqlite3 Normal file

Binary file not shown.

15
exemple/mon_site/manage.py Executable file
View file

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

View file

View file

@ -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/'

View file

@ -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')),
]

View file

@ -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()

374
formation.md Normal file
View file

@ -0,0 +1,374 @@
<!-- page_number: true -->
![](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
<Article: '' : None>
>>> from django.utils import timezone
>>> a.date = timezone.now()
>>> a.title = "Un super titre"
>>> a.text = "Un contenu vraiment très intéressant !"
>>> a
<Article: 'Un super titre' : 2018-04-07 12:34:01.509609+00:00>
>>> 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()
<QuerySet [<Article: 'Un super titre' : 2018-04-07>,
<Article: 'Un autre article' : 2018-04-07>]>
```
```python
>>> Article.objects.get(pk=1)
<Article: 'Un super titre' : 2018-04-07>
>>> Article.objects.order_by('date')
<QuerySet [<Article: 'Un super titre' : 2018-04-07>,
<Article: 'Un autre article' : 2018-04-07>]>
```
---
## Time to play !
```python
>>> import datetime
>>> d = datetime.timedelta(days=1)
>>> b.date += d
>>> b.save()
>>> Article.objects.filter(date__lte=timezone.now())
<QuerySet [<Article: 'Un super titre' : 2018-04-07>]>
```
---
# 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
<h3>Liste des articles</h3>
{% for article in articles %}
<div>
<h4>{{article.title}}</h4>
<p>Article écrit le {{article.date}}</p>
{% 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/)

BIN
formation.pdf Normal file

Binary file not shown.

BIN
logo-django.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

11
logo-django.svg Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="104px" height="36px" viewBox="0 0 104 36" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns">
<title>logo-django</title>
<description>Created with Sketch (http://www.bohemiancoding.com/sketch)</description>
<defs></defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">
<g id="logo-django" sketch:type="MSArtboardGroup" transform="translate(-8.000000, -5.000000)" fill="#FFFFFF">
<path d="M20.2602817,5 L25.9509859,5 L25.9509859,31.0824248 C23.0360563,31.6338042 20.8901408,31.8507285 18.5684507,31.8507285 C11.6180282,31.8434735 8,28.7383366 8,22.7747325 C8,17.0287781 11.8377465,13.2997118 17.7847887,13.2997118 C18.7076056,13.2997118 19.4107042,13.3722617 20.2602817,13.5899115 L20.2602817,5 L20.2602817,5 Z M20.2602817,18.1242821 C19.5938028,17.9066323 19.044507,17.8340823 18.3414085,17.8340823 C15.4630986,17.8340823 13.8005634,19.5897906 13.8005634,22.6666331 C13.8005634,25.6622196 15.3898592,27.316358 18.3047887,27.316358 C18.9346479,27.316358 19.4473239,27.2808085 20.2602817,27.1719836 L20.2602817,18.1242821 L20.2602817,18.1242821 Z M34.9960563,13.6987364 L34.9960563,26.7577235 C34.9960563,31.2550936 34.6591549,33.417807 33.6704225,35.2823401 C32.7476056,37.0750489 31.531831,38.2053768 29.0197183,39.453961 L23.7391549,36.9654985 C26.2512676,35.7981701 27.4670423,34.7665101 28.2433803,33.1921767 C29.056338,31.5822938 29.3126761,29.7177606 29.3126761,24.8133855 L29.3126761,13.6987364 L34.9960563,13.6987364 Z M29.3126761,5.02901997 L35.0033803,5.02901997 L35.0033803,10.8112493 L29.3126761,10.8112493 L29.3126761,5.02901997 Z M38.4302535,14.9828702 C40.9430986,13.8148163 43.3453521,13.2997118 45.9673239,13.2997118 C48.8895775,13.2997118 50.8077183,14.0687411 51.6580282,15.5705246 C52.1340845,16.4121037 52.2878873,17.5076077 52.2878873,19.8509704 L52.2878873,31.2993491 C49.7398873,31.6620987 46.5239437,31.922553 44.1649014,31.922553 C39.3970141,31.922553 37.2584225,30.2756696 37.2584225,26.6198787 C37.2584225,22.6659076 40.1008451,20.8376494 47.079831,20.2565245 L47.079831,19.0159207 C47.079831,17.9929667 46.559831,17.6229621 45.124338,17.6229621 C43.0223662,17.6229621 40.6567324,18.2106165 38.4375775,19.3423954 L38.4302535,14.9828702 Z M47.336169,23.9420608 C43.571662,24.3048105 42.3485634,24.8931904 42.3485634,26.3579734 C42.3485634,27.4549284 43.051662,27.9693073 44.604338,27.9693073 C45.4539155,27.9693073 46.2302535,27.8967574 47.3354366,27.7153826 L47.3354366,23.9420608 L47.336169,23.9420608 Z M55.056338,14.5765906 C58.4180282,13.6987364 61.1857465,13.2997118 63.9908169,13.2997118 C66.9057465,13.2997118 69.0157746,13.9599162 70.2674366,15.2367949 C71.4458592,16.4411237 71.8208451,17.7615324 71.8208451,20.5764696 L71.8208451,31.6258237 L66.1294085,31.6258237 L66.1294085,20.8013744 C66.1294085,18.6393866 65.3896901,17.8340823 63.3616901,17.8340823 C62.5846197,17.8340823 61.8822535,17.9066323 60.7397183,18.2403619 L60.7397183,31.6265492 L55.056338,31.6265492 L55.056338,14.5765906 Z M74.0326761,34.7012152 C76.0240563,35.7241692 78.0169014,36.1964692 80.1261972,36.1964692 C83.8540845,36.1964692 85.4433803,34.6946857 85.4433803,31.1107193 L85.4433803,31.0018944 C84.3374648,31.5460188 83.223493,31.7716491 81.7513803,31.7716491 C76.764507,31.7716491 73.5932394,28.5141573 73.5932394,23.3558574 C73.5932394,16.9496987 78.2878873,13.3294573 86.5932394,13.3294573 C89.0321127,13.3294573 91.2878873,13.583382 94.0189859,14.1347615 L92.0708169,18.1975575 C90.5562254,17.9073578 91.9463099,18.1540275 90.804507,18.0452026 L90.804507,18.6328571 L90.8777465,21.0124947 L90.9136338,24.0886117 C90.9509859,24.8583664 90.9509859,25.6259447 90.988338,26.3956994 L90.988338,27.9330324 C90.988338,32.7648576 90.5774648,35.0291409 89.3616901,36.900929 C87.5892958,39.6425908 84.5212958,41 80.1620845,41 C77.943662,41 76.0240563,40.6727998 74.0326761,39.9030451 L74.0326761,34.7012152 L74.0326761,34.7012152 Z M85.3335211,17.8703573 L85.1504225,17.8703573 L84.7395493,17.8703573 C83.6336338,17.8340823 82.3380282,18.1242821 81.4510986,18.6756615 C80.0895775,19.4446908 79.3872113,20.8376494 79.3872113,22.8110074 C79.3872113,25.6252192 80.7934085,27.2365531 83.3055211,27.2365531 C84.0811268,27.2365531 84.7117183,27.0921787 85.4441127,26.8738034 L85.4441127,26.4667983 L85.4441127,24.9294653 C85.4441127,24.269261 85.4067606,23.5365067 85.4067606,22.7674775 L85.3708732,20.17019 L85.3335211,18.3056569 L85.3335211,17.8703573 Z M102.84507,13.2271619 C108.528451,13.2271619 112,16.7748534 112,22.5208077 C112,28.4118619 108.382704,32.1039278 102.617296,32.1039278 C96.9265915,32.1039278 93.4176901,28.5569618 93.4176901,22.8480079 C93.4272113,16.9199532 97.044507,13.2271619 102.84507,13.2271619 Z M102.727887,27.5623023 C104.910423,27.5623023 106.199437,25.7710445 106.199437,22.6586526 C106.199437,19.5825356 104.94631,17.7542774 102.765239,17.7542774 C100.509465,17.7542774 99.2189859,19.5462607 99.2189859,22.6586526 C99.2189859,25.7710445 100.516056,27.5623023 102.727887,27.5623023 L102.727887,27.5623023 Z M102.727887,27.5623023" id="Shape" sketch:type="MSShapeGroup"></path>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 5.2 KiB

BIN
rezo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

BIN
vue_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

BIN
vue_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB