diff --git a/exemple/mon_site/blog/__pycache__/urls.cpython-36.pyc b/exemple/mon_site/blog/__pycache__/urls.cpython-36.pyc index faa8926..e6f6f7b 100644 Binary files a/exemple/mon_site/blog/__pycache__/urls.cpython-36.pyc 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 index 253f4d2..fadf811 100644 Binary files a/exemple/mon_site/blog/__pycache__/views.cpython-36.pyc and b/exemple/mon_site/blog/__pycache__/views.cpython-36.pyc differ diff --git a/exemple/mon_site/blog/templates/blog/list_articles.html b/exemple/mon_site/blog/templates/blog/list_articles.html new file mode 100644 index 0000000..6c0e2b7 --- /dev/null +++ b/exemple/mon_site/blog/templates/blog/list_articles.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} + +{% block content %} +

Liste des articles

+{% for article in articles %} +

{{article.title}}

+

Article écrit le {{article.date}}

+{% endfor %} +{% endblock %} diff --git a/exemple/mon_site/blog/templates/blog/view_article.html b/exemple/mon_site/blog/templates/blog/view_article.html new file mode 100644 index 0000000..f99d547 --- /dev/null +++ b/exemple/mon_site/blog/templates/blog/view_article.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} + +{% block content %} +

{{article.title}}

+Publié le {{article.date}}. +
+
+{{article.text}} +{% endblock %} diff --git a/exemple/mon_site/blog/urls.py b/exemple/mon_site/blog/urls.py index af127c5..67d6494 100644 --- a/exemple/mon_site/blog/urls.py +++ b/exemple/mon_site/blog/urls.py @@ -3,5 +3,6 @@ from . import views app_name = "blog" urlpatterns = [ - path('', views.index), + path('', views.index, name="index"), + path('article/', views.view_article, name="article") ] diff --git a/exemple/mon_site/blog/views.py b/exemple/mon_site/blog/views.py index fdf8bab..e3f2c2a 100644 --- a/exemple/mon_site/blog/views.py +++ b/exemple/mon_site/blog/views.py @@ -1,14 +1,12 @@ -from django.shortcuts import render +from django.shortcuts import render, get_object_or_404 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) + return render(request, 'blog/list_articles.html', {'articles':articles}) + +def view_article(request, pk): + article = get_object_or_404(Article, pk=pk) + return render(request, 'blog/view_article.html', {'article':article}) diff --git a/exemple/mon_site/mon_site/__pycache__/settings.cpython-36.pyc b/exemple/mon_site/mon_site/__pycache__/settings.cpython-36.pyc index 48d8f45..9074e24 100644 Binary files a/exemple/mon_site/mon_site/__pycache__/settings.cpython-36.pyc and b/exemple/mon_site/mon_site/__pycache__/settings.cpython-36.pyc differ diff --git a/exemple/mon_site/mon_site/settings.py b/exemple/mon_site/mon_site/settings.py index e91b735..c437715 100644 --- a/exemple/mon_site/mon_site/settings.py +++ b/exemple/mon_site/mon_site/settings.py @@ -55,7 +55,7 @@ ROOT_URLCONF = 'mon_site.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], + 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ diff --git a/exemple/mon_site/templates/base.html b/exemple/mon_site/templates/base.html new file mode 100644 index 0000000..3d154be --- /dev/null +++ b/exemple/mon_site/templates/base.html @@ -0,0 +1,12 @@ + + + + Mon super blog + + + +

Mon super titre qu'on verra partout

+ Retour à l'accueil + {% block content %}{% endblock %} + + diff --git a/formation.md b/formation.md index 3b452f6..e8e4e1a 100644 --- a/formation.md +++ b/formation.md @@ -341,6 +341,7 @@ def index(request): --- # L'architecture MVT ## T comme Templates +Dans `blog/templates/blog/list_articles.html`: ```html

Liste des articles

{% for article in articles %} @@ -349,6 +350,220 @@ def index(request):

Article écrit le {{article.date}}

{% endfor %} ``` +--- +## T comme Templates +Dans `blog/views.py` : +```python +from django.shortcuts import render +from django.http import HttpResponse + +from .models import Article + +def index(request): + articles = Article.objects.order_by('-date') + return render( + request, + 'blog/list_articles.html', + {'articles':articles} + ) +``` +--- + +## Votre site : + +![](vue_3.png) + +--- + +## Vous : +![](https://tr2.cbsistatic.com/hub/i/2014/05/15/f8964afd-bd82-4e0e-bcbe-e927363dcdc1/3b858e39e2cf183b878f54cad0073a67/codedoge.jpg) + +--- + +## Étendre un template +Dans `templates/base.html` : +```html + + + + Mon super blog + + + +

Mon super titre qu'on verra partout

+ {% block content %}{% endblock %} + + +``` + +--- +## Étendre un template +Dans `mon_site/settings.py` (ligne 55): + +```python +#... +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [os.path.join(BASE_DIR, 'templates')], + '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', + ], + }, + }, +] +#... +``` + +--- +## Étendre un template +Dans `blog/templates/blog/list_articles.html` +```python +{% extends 'base.html' %} + +{% block content %} +

Liste des articles

+{% for article in articles %} +
+

{{article.title}}

+

Article écrit le {{article.date}}

+{% endfor %} +{% endblock %} +``` + +--- +## Votre site +![](vue_4.png) + +--- + +## Exercice : afficher un article +Objectif : +![](vue_5.png) + +--- + +## Exercice : afficher un article + +- Créer la vue dédiée (`def view_article(request, pk):`) +- La remplir (conseil regarder `django.shortcuts.get_object_or_404`) +- Créer l'url dédiée dans `blog/urls.py` (elle sera de la forme `article/`) +- Créer le template associé (dans `blog/templates/blog/view_article.html`) + +--- + +## Ma solution +Dans `blog/views.py` : +```python +def view_article(request, pk): + article = get_object_or_404(Article, pk=pk) + return render( + request, + 'blog/view_article.html', + {'article':article} + ) +``` +Dans `blog/urls.py` : +```python +path('article/', views.view_article) +``` +--- +## Ma solution +Dans `blog/templates/blog/view_article.html`: +```html +{% extends 'base.html' %} + +{% block content %} +

{{article.title}}

+Publié le {{article.date}}. +
+
+{{article.text}} +{% endblock %} +``` + +--- +## Tags + +### → Commandes pour les templates +Exemple : `{% url %}` + +Dans `blog/urls.py` : +```python +from django.urls import path +from . import views + +app_name = "blog" +urlpatterns = [ + path('', views.index, name="index"), + path( + 'article/', + views.view_article, + name="article" + ) +] +``` + +--- +## Tags + +Dans `blog/templates/blog/list_articles.html` : +```html +{% extends 'base.html' %} + +{% block content %} +

Liste des articles

+{% for article in articles %} +

+ + {{article.title}} + +

+

Article écrit le {{article.date}}

+{% endfor %} +{% endblock %} +``` +--- +## Tags + +Dans `templates/base.html` : +```html + + + + Mon super blog + + + +

Mon super titre qu'on verra partout

+ + Retour à l'accueil + + {% block content %}{% endblock %} + + +``` +--- + +![](vue_7.png) + +--- + +![](vue_6.png) + +--- + +# Site admin + +--- + +# Forms + --- # Sites intéressants diff --git a/formation.pdf b/formation.pdf index 5a0a5c3..d40058c 100644 Binary files a/formation.pdf and b/formation.pdf differ diff --git a/vue_3.png b/vue_3.png new file mode 100644 index 0000000..c9871ef Binary files /dev/null and b/vue_3.png differ diff --git a/vue_4.png b/vue_4.png new file mode 100644 index 0000000..bcf3dd9 Binary files /dev/null and b/vue_4.png differ diff --git a/vue_5.png b/vue_5.png new file mode 100644 index 0000000..d56dd75 Binary files /dev/null and b/vue_5.png differ diff --git a/vue_6.png b/vue_6.png new file mode 100644 index 0000000..61571cb Binary files /dev/null and b/vue_6.png differ diff --git a/vue_7.png b/vue_7.png new file mode 100644 index 0000000..54e604f Binary files /dev/null and b/vue_7.png differ