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

cours 2 ok

parent d383e529
......@@ -12,6 +12,12 @@
- AFaire
- Entretien
### un venv
~~~
$ source /opt/venv/django/bin/activate
$ pipenv run ...
~~~
## Développement
### Lancement du serveur de développement
~~~
......@@ -20,7 +26,7 @@ Performing system checks...
System check identified no issues (0 silenced).
September 28, 2017 - 13:29:48
Django version 1.11, using settings 'monsite.settings'
Django version 1.11, using settings 'conf.settings'
Starting development server at http://127.0.0.1:8000/
~~~
......@@ -67,7 +73,7 @@ admin.site.register(Entretien, EntretienAdmin)
# MVC
## Modèle Vue Contrôleur
## Modèle Vue Contrôleur (~MTV)
### Modèle
- garage/models.py
......@@ -78,12 +84,13 @@ admin.site.register(Entretien, EntretienAdmin)
### Vue
- garage/templates
- garage/views.py
. . .
### Contrôleur
- Django
- garage/views.py
- garage/urls.py
## Définition
......@@ -92,8 +99,8 @@ admin.site.register(Entretien, EntretienAdmin)
https://fr.wikipedia.org/wiki/Modèle-vue-contrôleur
~~~
# Contrôle
## Contrôle
# 1er page web
## Vue
### garage/views.py
~~~python
from django.http import HttpResponse
......@@ -104,8 +111,6 @@ def home(request):
return HttpResponse('ça marche !')
~~~
# Vue
## Routage d'URLs
### conf/urls.py
~~~python
......@@ -173,7 +178,7 @@ INSTALLED_APPS = [
~~~
## garage/templates/garage/home.html
### head (v1)
### body (v1)
~~~html
<body>
{% block content %}<p>Bienvenue !</p>{% endblock %}
......@@ -197,7 +202,7 @@ def home(request):
~~~
## garage/templates/garage/home.html
### avec navbar (v2), responsive design ?
### avec navbar (v2)
~~~html
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="/">Garage</a>
......@@ -224,7 +229,7 @@ def home(request):
{% endif %}
~~~
## Connexion
## Connexion des utilisateurs
### conf/urls.py
~~~python
# ...
......@@ -232,9 +237,63 @@ path('accounts/', include('django.contrib.auth.urls')),
# ...
~~~
TODO: faire la vue pour voir les garages avec login_required
### garage/templates/registration/login.html
- le template doit être présent
### garage/urls.py
~~~python
from django.contrib.auth.decorators import login_required
~~~
## Afficher la liste des garages
\center\includegraphics[height=5cm]{supports_cours_02/get_url.png}
## Les vues génériques (views)
### garage/views.py
~~~python
from django.views.generic import ListView
class GaragesListView(ListView):
model = Garage
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['menu_garages'] = 'active'
return context
~~~
## Les vues génériques (templates)
### garage/templates/garage/garage_list.html
~~~python
{% extends "garage/home.html" %}
{% load staticfiles %}
{% block content %}
...
~~~
## Les vues génériques (urls)
### garage/urls.py
~~~python
from django.contrib.auth.decorators import login_required
app_name = "garage"
...
path('garages/',
login_required(views.GaragesListView.as_view()),
name='garages'),
...
~~~
## Les vues génériques (templates)
### garage/templates/garage/home.html
~~~python
<li class="nav-item {{ menu_garages }}">
<a class="nav-link" href="{% url 'garage:garages' %}">Garages</a>
</li>
~~~
## Bonnes pratiques
## Bonnes pratiques pour la partie Front
- Pensez à utiliser le mode développeur (F12) !
- CDN, attention aux dépendances
- Pensez à utiliser le mode développeur (F12)
- CDN, attention aux dépendances extérieures
- limité le chargement des JS
No preview for this file type
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
django = "*"
"psycopg2-binary" = "*"
ipython = "*"
django-hijack = "*"
django-hijack-admin = "*"
django-extensions = "*"
django-bootstrap-static = "*"
[dev-packages]
pytest = "*"
[requires]
python_version = "3.6"
from django.db import models
from django.contrib.auth.models import User
def user_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return f"user_{instance.user.id}/{filename}"
class Garage(models.Model):
nom = models.CharField(max_length=32)
adresse = models.CharField(max_length=128, null=True, blank=True)
mail = models.EmailField(null=True, blank=True)
telephone = models.CharField(max_length=12, null=True, blank=True)
createur = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta(object):
ordering = ("nom", "adresse")
def __str__(self):
return self.nom
class Vehicule(models.Model):
marque = models.CharField(max_length=32)
modele = models.CharField(max_length=32)
immatriculation = models.CharField(max_length=32, null=True, blank=True)
proprietaire = models.ForeignKey(User, on_delete=models.CASCADE)
mise_en_circulation = models.IntegerField(default=0)
visible = models.BooleanField(default=False)
class Meta(object):
ordering = ("marque", "modele")
def __str__(self):
msg = f"{self.marque} {self.modele}"
if self.immatriculation:
return f"{msg} ({self.immatriculation})"
return msg
class AFaire(models.Model):
description = models.CharField(max_length=64)
date = models.DateField(null=True, blank=True)
kms = models.IntegerField(null=True, blank=True)
vehicule = models.ForeignKey(Vehicule, on_delete=models.CASCADE)
createur = models.ForeignKey(User, on_delete=models.CASCADE)
class Entretien(models.Model):
garage = models.ForeignKey(Garage, on_delete=models.CASCADE)
vehicule = models.ForeignKey(Vehicule, on_delete=models.CASCADE)
kms = models.IntegerField()
date = models.DateField()
montant = models.DecimalField(decimal_places=2, max_digits=8)
facture = models.FileField(upload_to=user_directory_path, null=True, blank=True)
createur = models.ForeignKey(User, on_delete=models.CASCADE)
{% extends "garage/home.html" %}
{% load staticfiles %}
{% block content %}
<h2>Liste des garages <a href="#" class="btn btn-success active" role="button" aria-pressed="true">Ajouter</a></h2>
<table class="table table-striped table-bordered" cellspacing="0">
<thead>
<tr><th>Nom</th>
<th>Adresse</th>
<th>Téléphone</th>
</tr>
</thead>
<tbody>
{% for grg in garages %}
{{grg}}
<tr><td>{{ grg.nom }}</td><td>{{ grg.adresse }}</td><td>{{ grg.telephone }}</td></tr>
{% empty %}
<tr><td colspan="3">Aucun garage pour le moment</td></tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
This diff is collapsed.
{% extends "garage/home.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login">
<input type="hidden" name="next" value="{{ next }}">
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% endblock %}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment