commit
b8e8772c1b
34 changed files with 633 additions and 0 deletions
|
After Width: | Height: | Size: 43 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,3 @@ |
|||||
|
from django.contrib import admin |
||||
|
|
||||
|
# Register your models here. |
||||
@ -0,0 +1,5 @@ |
|||||
|
from django.apps import AppConfig |
||||
|
|
||||
|
|
||||
|
class BlogConfig(AppConfig): |
||||
|
name = 'blog' |
||||
@ -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')), |
||||
|
], |
||||
|
), |
||||
|
] |
||||
Binary file not shown.
Binary file not shown.
@ -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 |
||||
|
) |
||||
@ -0,0 +1,3 @@ |
|||||
|
from django.test import TestCase |
||||
|
|
||||
|
# Create your tests here. |
||||
@ -0,0 +1,7 @@ |
|||||
|
from django.urls import path |
||||
|
from . import views |
||||
|
|
||||
|
app_name = "blog" |
||||
|
urlpatterns = [ |
||||
|
path('', views.index), |
||||
|
] |
||||
@ -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) |
||||
Binary file not shown.
@ -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) |
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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/' |
||||
@ -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')), |
||||
|
] |
||||
@ -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() |
||||
@ -0,0 +1,374 @@ |
|||||
|
<!-- page_number: true --> |
||||
|
 |
||||
|
|
||||
|
---- |
||||
|
|
||||
|
#  |
||||
|
|
||||
|
# 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 |
||||
|
|
||||
|
--- |
||||
|
|
||||
|
 |
||||
|
|
||||
|
--- |
||||
|
|
||||
|
# 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 |
||||
|
|
||||
|
---- |
||||
|
|
||||
|
 |
||||
|
|
||||
|
--- |
||||
|
|
||||
|
# 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 |
||||
|
``` |
||||
|
--- |
||||
|
|
||||
|
 |
||||
|
|
||||
|
--- |
||||
|
# 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 ? |
||||
|
|
||||
|
 |
||||
|
|
||||
|
--- |
||||
|
|
||||
|
# 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 : |
||||
|
|
||||
|
 |
||||
|
|
||||
|
--- |
||||
|
## 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 : |
||||
|
 |
||||
|
### Vous : |
||||
|
 |
||||
|
|
||||
|
--- |
||||
|
# 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/) |
||||
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
|
After Width: | Height: | Size: 5.2 KiB |
|
After Width: | Height: | Size: 104 KiB |
|
After Width: | Height: | Size: 7.8 KiB |
|
After Width: | Height: | Size: 11 KiB |
Loading…
Reference in new issue