Commit b5cbcdaf authored by Bonnegent Sebastien's avatar Bonnegent Sebastien
Browse files

cours 1 ok :)

parent 1cc9f03a
Pipeline #8666 failed with stage
in 16 seconds
*~
html/*.html
html/images/
env
all: html/cours1.html html/cours2.html html/cours3.html html/cours4.html html/cours5.html html/cours6.html html/cours7.html
html/%.html: %.md
@# pandoc -s -t S5 -o $@ $<
pandoc -s -t revealjs -V revealjs-url=./reveal.js-3.9.2 -V theme=moon -o $@ $<
@# pandoc -s -t revealjs -V revealjs-url=./reveal.js-4.1.0/js/ -o $@ $<
@test ! -e html/images && mkdir html/images
@cp images/* html/images/
pandoc -s -t revealjs -V revealjs-url=./reveal.js-3.9.2 -V theme=moon -o $@ $< --verbose
themes:
pandoc -s -t revealjs -V revealjs-url=./reveal.js-3.9.2 -V theme=beige -o html/cours1.beige.html cours1.md
......@@ -19,5 +19,8 @@ themes:
pandoc -s -t revealjs -V revealjs-url=./reveal.js-3.9.2 -V theme=white -o html/cours1.white.html cours1.md
firefox html/cours1.*.html
cours%:
firefox --new-window html/$@.html
clean:
rm -f html/cours?.html html/cours1.*.html
rm -f html/cours?.html html/cours1.*.html html/images/*
......@@ -2,13 +2,14 @@
author:
- Sébastien Bonnegent
title: Django par la pratique 1/7
lang: fr
---
# Django
## Qu'est ce que c'est ?
> The web framework for perfectionists with deadlines.[^dj]
[^dj]: \tiny https://www.djangoproject.com/
[^dj]: https://www.djangoproject.com/
- framework web python de haut niveau
- rapide à mettre en place
......@@ -36,37 +37,42 @@ title: Django par la pratique 1/7
Sources: [^gen] [^star]
[^gen]: \tiny https://docs.djangoproject.com/fr/3.1/faq/general/#is-django-stable
[^star]: \tiny https://djangostars.com/blog/10-popular-sites-made-on-django/
[^gen]: https://docs.djangoproject.com/fr/3.1/faq/general/#is-django-stable
[^star]: https://djangostars.com/blog/10-popular-sites-made-on-django/
# Prérequis: python
## Système
# Prérequis
## python: système
### plus
- rapide
### moins
- pas la dernière version
- dépendant de la distribution (poste dév != production)
- version imposée
- complique le déploiement
## pyenv
## python: pyenv
### plus
- rapide
- n'importe quelle version
- permet de gérer plusieurs versions
### moins
- plus compliqué pour la production (dépend de l'utilisateur)
- plus compliquer en production
## virtualenv
(variable d'environnement dans l'espace utilisateur)
## python: virtualenv
- virtualisation légère
- utilisation de pip
- `python3 -m venv env`
`source env/bin/activate`
`deactivate`
## docker
https://hub.docker.com/_/python/
https://docs.docker.com/compose/django/
~~~bash
$ python3 -m venv env
$ source env/bin/activate
$ deactivate
~~~
## python: docker
[Docker Python](https://hub.docker.com/_/python/) /
[Docker Django](https://docs.docker.com/compose/django/)
### plus
- indépendance du système
......@@ -77,8 +83,8 @@ https://docs.docker.com/compose/django/
- nécessite des droits root (ou équivalent)
- légèrement moins rapide
## podman
https://podman.io/
## python: podman
[Podman](https://podman.io/)
### plus
- indépendance du système
......@@ -94,27 +100,35 @@ https://podman.io/
- niveau système ou niveau utilisateur
- pas possible d'avoir plusieurs environnements
- gestion des dépendances (requirements.txt)
- `pip install -r requirements.txt`
~~~bash
$ pip install -r requirements.txt
~~~
## Bibliothèques
### pipenv [^pipenv]
- utilise pip et virtualenv
- fichier Pipfile pour gérer les bibliothèques
- `pipenv install --dev`
- `pipenv run python ./manage.py`
~~~bash
$ pipenv install --dev
$ pipenv run python ./manage.py
~~~
[^pipenv]: https://gitlab.insa-rouen.fr/bonnegent/cours_django/wikis/pipenv
## Les supports
~~~
[Cours 1](https://gitlab.insa-rouen.fr/bonnegent/cours_django/-/blob/master/cours1.md)
~~~bash
$ git clone https://gitlab.insa-rouen.fr/bonnegent/cours_django.git
$ cd cours_django
$ ls exemples
docker-compose.yml Dockerfile Pipfile Pipfile.lock requirements.txt
$ mkdir fwm1
$ cd !$
~~~
https://gitlab.insa-rouen.fr/bonnegent/cours_django/-/blob/master/cours1.md
## Projet ou application ?
> Une application est une application Web qui fait
> quelque chose – par exemple un système de blog,
......@@ -125,16 +139,16 @@ https://gitlab.insa-rouen.fr/bonnegent/cours_django/-/blob/master/cours1.md
> plusieurs applications.
> Une application peut apparaître dans plusieurs projets.[^ted]
[^ted]: \tiny https://docs.djangoproject.com/fr/3.1/intro/tutorial01/
[^ted]: https://docs.djangoproject.com/fr/3.1/intro/tutorial01/
## Création du projet
~~~
~~~bash
$ django-admin startproject conf .
~~~
![](images/tree-conf.png)
## Création d'une application
~~~
~~~bash
$ django-admin startapp webui
~~~
![](images/tree-webui.png)
......@@ -166,37 +180,41 @@ TIME_ZONE = 'Europe/Paris'
SECRET_KEY = 'Une_clef_secrete_et_longue!'
~~~
## Configuration
### Pour générer une nouvelle clé secrête
~~~
~~~bash
$ ./manage.py generate_secret_key
f#wgrz$@#wh@*x5)_+(*$$^aw(c!q#a4)c0230bwp!iy
~~~
## Gestion des migrations
### Création (si nécessaire)
~~~
~~~bash
$ ./manage.py makemigrations
~~~
## Gestion des migrations
### Application
~~~
~~~bash
$ ./manage.py migrate
~~~
![](images/tree-migrate-initial.png)
## Quelques commandes utiles
~~~
~~~bash
$ ./manage.py shell
$ # shell avec chargement automatique des modèles
$ # mise à dispo par django_extensions
$ ./manage.py shell_plus
~~~
~~~
$ ./manage.py createsuperuser
~~~bash
$ ./manage.py createsuperuser --username admin1 --email ad@min.fr
~~~
## Quelques commandes utiles
### Serveur de développement
~~~
~~~bash
$ ./manage.py runserver
Performing system checks...
......@@ -237,7 +255,7 @@ class Port(models.Model):
~~~
## Appliquer les modifications
~~~
~~~bash
$ ./manage.py makemigrations
Migrations for 'webui':
webui/migrations/0001_initial.py
......@@ -247,7 +265,7 @@ $ ./manage.py migrate
~~~
## Contenu d'une migration
~~~
~~~bash
$ cat webui/migrations/0001_initial.py
~~~
......@@ -271,8 +289,7 @@ class Migration(migrations.Migration):
]
~~~
## Shell django
### ./manage.py shell
## ./manage.py shell
~~~python
>>> from webui.models import Port
>>> p = Port()
......@@ -303,8 +320,7 @@ class Port(models.Model):
return f"{ports}({self.get_protocole_display()})"
~~~
## Shell plus
### ./manage.py shell_plus
## ./manage.py shell\_plus
~~~python
>>> p = Port(debut=80, protocole="tcp", fin=88)
>>> p
......@@ -324,9 +340,9 @@ class Port(models.Model):
Modifier l'ordre des instances.
~~~python
class Port(models.Model):
class Meta(object):
class Port(models.Model):
class Meta(object):
ordering = ('debut', 'protocole', 'fin')
~~~
......@@ -361,7 +377,10 @@ Out[2]: 2
In [3]: Port.objects.filter(debut=80)[1]
Out[3]: <Port: 80-88(tcp)>
~~~
## Sélection sur attributs
~~~python
In [4]: Port.objects.filter(debut__gt=20)
Out[4]: <QuerySet [<Port: 22(tcp)>, <Port: 25(tcp)>,
<Port: 80(tcp)>, <Port: 80-88(tcp)>]>
......@@ -402,8 +421,8 @@ Faire la classe suivante
# Documentation
## Django
- https://docs.djangoproject.com/fr/
- <https://docs.djangoproject.com/fr/>
- partie modèle
- https://docs.djangoproject.com/fr/2.2/intro/tutorial01/
- <https://docs.djangoproject.com/fr/3.1/intro/tutorial01/>
- tutoriel officiel
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
url = "https://pypi.python.org/simple"
verify_ssl = true
[dev-packages]
werkzeug = "*"
black = "==18.6b4"
pytest-django = "*"
pytest-xdist = "*"
name = "pypi"
[packages]
ipython = "*"
django = "*"
django-extensions = "*"
ipython = "*"
django-bootstrap-static = "*"
fontawesome = "*"
psycopg2-binary = "*"
werkzeug = "*"
pytest-django = "*"
pytest-xdist = "*"
black = "==18.6b4"
[dev-packages]
[requires]
python_version = "3.8"
{
"_meta": {
"hash": {
"sha256": "b5631265c8002334874e01bb86394626fd56f09c2ff2935eb10b0eef5279ab98"
"sha256": "49190d6ccfd08b78e5ce9674f554371a7d45c034a5d2d41fa63e3de5b8ef11c5"
},
"pipfile-spec": 6,
"requires": {
......@@ -10,18 +10,39 @@
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"url": "https://pypi.python.org/simple",
"verify_ssl": true
}
]
},
"default": {
"apipkg": {
"hashes": [
"sha256:37228cda29411948b422fae072f57e31d3396d2ee1c9783775980ee9c9990af6",
"sha256:58587dd4dc3daefad0487f6d9ae32b4542b185e1c36db6993290e7c41ca2b47c"
],
"version": "==1.5"
},
"appdirs": {
"hashes": [
"sha256:7d5d0167b2b1ba821647616af46a749d1c653740dd0d2415100fe26e27afdf41",
"sha256:a841dacd6b99318a741b166adb07e19ee71a274450e68237b4650ca1055ab128"
],
"version": "==1.4.4"
},
"asgiref": {
"hashes": [
"sha256:a5098bc870b80e7b872bff60bb363c7f2c2c89078759f6c47b53ff8c525a152e",
"sha256:cd88907ecaec59d78e4ac00ea665b03e571cb37e3a0e37b3702af1a9e86c365a"
"sha256:5ee950735509d04eb673bd7f7120f8fa1c9e2df495394992c73234d526907e17",
"sha256:7162a3cb30ab0609f1a4c95938fd73e8604f63bdba516a7f7d64b83ff09478f0"
],
"version": "==3.3.1"
},
"attrs": {
"hashes": [
"sha256:31b2eced602aa8423c2aea9c76a724617ed67cf9513173fd3a4f03e3a929c7e6",
"sha256:832aa3cde19744e49938b91fea06d69ecb9e649c93ba974535d08ad92164f700"
],
"version": "==3.3.0"
"version": "==20.3.0"
},
"backcall": {
"hashes": [
......@@ -30,6 +51,21 @@
],
"version": "==0.2.0"
},
"black": {
"hashes": [
"sha256:22158b89c1a6b4eb333a1e65e791a3f8b998cf3b11ae094adb2570f31f769a44",
"sha256:4b475bbd528acce094c503a3d2dbc2d05a4075f6d0ef7d9e7514518e14cc5191"
],
"index": "pypi",
"version": "==18.6b4"
},
"click": {
"hashes": [
"sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a",
"sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc"
],
"version": "==7.1.2"
},
"decorator": {
"hashes": [
"sha256:41fa54c2a0cc4ba648be4fd43cff00aedf5b9465c9bf18d64325bc225f08f760",
......@@ -61,6 +97,13 @@
"index": "pypi",
"version": "==3.0.9"
},
"execnet": {
"hashes": [
"sha256:cacb9df31c9680ec5f95553976c4da484d407e85e41c83cb812aa014f0eddc50",
"sha256:d4efd397930c46415f62f8a31388d6be4f27a91d7550eb79bc64a756e0056547"
],
"version": "==1.7.1"
},
"fontawesome": {
"hashes": [
"sha256:1b5e8f2ed12a74b88aef6f404ff770af425324041bc5620523a802d5406595b8",
......@@ -69,6 +112,13 @@
"index": "pypi",
"version": "==5.10.1.post1"
},
"iniconfig": {
"hashes": [
"sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3",
"sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32"
],
"version": "==1.1.1"
},
"ipython": {
"hashes": [
"sha256:c987e8178ced651532b3b1ff9965925bfd445c279239697052561a9ab806d28f",
......@@ -91,6 +141,13 @@
],
"version": "==0.17.2"
},
"packaging": {
"hashes": [
"sha256:4357f74f47b9c12db93624a82154e9b120fa8293699949152b22065d556079f8",
"sha256:998416ba6962ae7fbd6596850b80e17859a5753ba17c32284f67bfff33784181"
],
"version": "==20.4"
},
"parso": {
"hashes": [
"sha256:97218d9159b2520ff45eb78028ba8b50d2bc61dcc062a9682666f2dc4bd331ea",
......@@ -113,6 +170,13 @@
],
"version": "==0.7.5"
},
"pluggy": {
"hashes": [
"sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0",
"sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"
],
"version": "==0.13.1"
},
"prompt-toolkit": {
"hashes": [
"sha256:25c95d2ac813909f813c93fde734b6e44406d1477a9faef7c915ff37d39c0a8c",
......@@ -125,9 +189,11 @@
"sha256:0deac2af1a587ae12836aa07970f5cb91964f05a7c6cdb69d8425ff4c15d4e2c",
"sha256:0e4dc3d5996760104746e6cfcdb519d9d2cd27c738296525d5867ea695774e67",
"sha256:11b9c0ebce097180129e422379b824ae21c8f2a6596b159c7659e2e5a00e1aa0",
"sha256:15978a1fbd225583dd8cdaf37e67ccc278b5abecb4caf6b2d6b8e2b948e953f6",
"sha256:1fabed9ea2acc4efe4671b92c669a213db744d2af8a9fc5d69a8e9bc14b7a9db",
"sha256:2dac98e85565d5688e8ab7bdea5446674a83a3945a8f416ad0110018d1501b94",
"sha256:42ec1035841b389e8cc3692277a0bd81cdfe0b65d575a2c8862cec7a80e62e52",
"sha256:6422f2ff0919fd720195f64ffd8f924c1395d30f9a495f31e2392c2efafb5056",
"sha256:6a32f3a4cb2f6e1a0b15215f448e8ce2da192fd4ff35084d80d5e39da683e79b",
"sha256:7312e931b90fe14f925729cde58022f5d034241918a5c4f9797cac62f6b3a9dd",
"sha256:7d92a09b788cbb1aec325af5fcba9fed7203897bbd9269d5691bb1e3bce29550",
......@@ -166,107 +232,6 @@
],
"version": "==0.6.0"
},
"pygments": {
"hashes": [
"sha256:381985fcc551eb9d37c52088a32914e00517e57f4a21609f48141ba08e193fa0",
"sha256:88a0bbcd659fcb9573703957c6b9cff9fab7295e6e76db54c9d00ae42df32773"
],
"version": "==2.7.2"
},
"pytz": {
"hashes": [
"sha256:3e6b7dd2d1e0a59084bcee14a17af60c5c562cdc16d828e8eba2e683d3a7e268",
"sha256:5c55e189b682d420be27c6995ba6edce0c0a77dd67bfbe2ae6607134d5851ffd"
],
"version": "==2020.4"
},
"sqlparse": {
"hashes": [
"sha256:017cde379adbd6a1f15a61873f43e8274179378e95ef3fede90b5aa64d304ed0",
"sha256:0f91fd2e829c44362cbcfab3e9ae12e22badaa8a29ad5ff599f9ec109f0454e8"
],
"version": "==0.4.1"
},
"traitlets": {
"hashes": [
"sha256:178f4ce988f69189f7e523337a3e11d91c786ded9360174a3d9ca83e79bc5396",
"sha256:69ff3f9d5351f31a7ad80443c2674b7099df13cc41fc5fa6e2f6d3b0330b0426"
],
"version": "==5.0.5"
},
"wcwidth": {
"hashes": [
"sha256:beb4802a9cebb9144e99086eff703a642a13d6a0052920003a230f3294bbe784",
"sha256:c4d647b99872929fdb7bdcaa4fbe7f01413ed3d98077df798530e5b04f116c83"
],
"version": "==0.2.5"
}
},
"develop": {
"apipkg": {
"hashes": [
"sha256:37228cda29411948b422fae072f57e31d3396d2ee1c9783775980ee9c9990af6",
"sha256:58587dd4dc3daefad0487f6d9ae32b4542b185e1c36db6993290e7c41ca2b47c"
],
"version": "==1.5"
},
"appdirs": {
"hashes": [
"sha256:7d5d0167b2b1ba821647616af46a749d1c653740dd0d2415100fe26e27afdf41",
"sha256:a841dacd6b99318a741b166adb07e19ee71a274450e68237b4650ca1055ab128"
],
"version": "==1.4.4"
},
"attrs": {
"hashes": [
"sha256:26b54ddbbb9ee1d34d5d3668dd37d6cf74990ab23c828c2888dccdceee395594",
"sha256:fce7fc47dfc976152e82d53ff92fa0407700c21acd20886a13777a0d20e655dc"
],
"version": "==20.2.0"
},
"black": {
"hashes": [
"sha256:22158b89c1a6b4eb333a1e65e791a3f8b998cf3b11ae094adb2570f31f769a44",
"sha256:4b475bbd528acce094c503a3d2dbc2d05a4075f6d0ef7d9e7514518e14cc5191"
],
"index": "pypi",
"version": "==18.6b4"
},
"click": {
"hashes": [
"sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a",
"sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc"
],
"version": "==7.1.2"
},
"execnet": {
"hashes": [
"sha256:cacb9df31c9680ec5f95553976c4da484d407e85e41c83cb812aa014f0eddc50",
"sha256:d4efd397930c46415f62f8a31388d6be4f27a91d7550eb79bc64a756e0056547"
],
"version": "==1.7.1"
},
"iniconfig": {
"hashes": [
"sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3",
"sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32"
],
"version": "==1.1.1"
},
"packaging": {
"hashes": [
"sha256:4357f74f47b9c12db93624a82154e9b120fa8293699949152b22065d556079f8",
"sha256:998416ba6962ae7fbd6596850b80e17859a5753ba17c32284f67bfff33784181"
],
"version": "==20.4"
},
"pluggy": {
"hashes": [
"sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0",
"sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"
],
"version": "==0.13.1"
},
"py": {
"hashes": [
"sha256:366389d1db726cd2fcfc79732e75410e5fe4d31db13692115529d34069a043c2",
......@@ -274,6 +239,13 @@
],
"version": "==1.9.0"
},
"pygments": {
"hashes": [
"sha256:381985fcc551eb9d37c52088a32914e00517e57f4a21609f48141ba08e193fa0",
"sha256:88a0bbcd659fcb9573703957c6b9cff9fab7295e6e76db54c9d00ae42df32773"
],
"version": "==2.7.2"
},
"pyparsing": {
"hashes": [
"sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1",
......@@ -311,6 +283,13 @@
"index": "pypi",
"version": "==2.1.0"
},
"pytz": {
"hashes": [
"sha256:3e6b7dd2d1e0a59084bcee14a17af60c5c562cdc16d828e8eba2e683d3a7e268",
"sha256:5c55e189b682d420be27c6995ba6edce0c0a77dd67bfbe2ae6607134d5851ffd"
],
"version": "==2020.4"
},
"six": {
"hashes": [
"sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259",
......@@ -318,6 +297,13 @@
],
"version": "==1.15.0"
},
"sqlparse": {
"hashes": [
"sha256:017cde379adbd6a1f15a61873f43e8274179378e95ef3fede90b5aa64d304ed0",
"sha256:0f91fd2e829c44362cbcfab3e9ae12e22badaa8a29ad5ff599f9ec109f0454e8"
],
"version": "==0.4.1"
},
"toml": {
"hashes": [
"sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b",
......@@ -325,6 +311,20 @@
],
"version": "==0.10.2"
},
"traitlets": {
"hashes": [
"sha256:178f4ce988f69189f7e523337a3e11d91c786ded9360174a3d9ca83e79bc5396",
"sha256:69ff3f9d5351f31a7ad80443c2674b7099df13cc41fc5fa6e2f6d3b0330b0426"
],
"version": "==5.0.5"
},
"wcwidth": {
"hashes": [
"sha256:beb4802a9cebb9144e99086eff703a642a13d6a0052920003a230f3294bbe784",
"sha256:c4d647b99872929fdb7bdcaa4fbe7f01413ed3d98077df798530e5b04f116c83"
],
"version": "==0.2.5"
},
"werkzeug": {
"hashes": [
"sha256:2de2a5db0baeae7b2d2664949077c2ac63fbd16d98da0ff71837f7d1dea3fd43",
......@@ -333,5 +333,6 @@
"index": "pypi",
"version": "==1.0.1"
}
}
},
"develop": {}
}
......@@ -23,6 +23,7 @@ class Adresse(models.Model):
:param ip: adresse IPv4 ou v6
:param masque: le masque associé, /32 par défaut pour une adresse unique
"""
nom = models.CharField(default='', max_length=25)
ip = models.GenericIPAddressField(default="0.0.0.0")
masque = models.PositiveSmallIntegerField(default=32)
......