Projet DJANGO
Ce projet consiste à développer une application simple de gestion de bibliothèque. Les
fonctionnalités incluent l’ajout, la gestion et la recherche de livres.
Projet : Gestion de Bibliothèque
Objectifs :
1. Mettre en pratique les concepts de POO en Python.
2. Utiliser Django pour créer une application web.
3. Gérer des données de manière dynamique via une base de données.
Fonctionnalités :
Ajouter des livres avec titre, auteur, date de publication, genre.
Lister tous les livres disponibles.
Rechercher des livres par titre ou auteur.
Supprimer ou mettre à jour des livres.
Étapes de réalisation :
1. Création de l'environnement Django
1. Installez Django :
pip install django
2. Créez un projet Django nommé LibraryManagement :
django-admin startproject LibraryManagement
3. Créez une application Django nommée books :
python [Link] startapp books
2. Configuration du projet
Dans LibraryManagement/[Link], ajoutez books à INSTALLED_APPS :
INSTALLED_APPS = [
...
'books',
]
3. Création des modèles (POO)
Dans books/[Link], définissez un modèle pour les livres :
from [Link] import models
class Book([Link]):
title = [Link](max_length=200)
author = [Link](max_length=100)
publication_date = [Link]()
genre = [Link](max_length=50)
def __str__(self):
return [Link]
*Appliquez les migrations :
python [Link] makemigrations
python [Link] migrate
4. Création des vues
Dans books/[Link], implémentez les fonctionnalités de liste, ajout, recherche et
suppression :
from [Link] import render, get_object_or_404, redirect
from .models import Book
def book_list(request):
books = [Link]()
return render(request, 'books/book_list.html', {'books': books})
def book_add(request):
if [Link] == 'POST':
title = [Link]['title']
author = [Link]['author']
publication_date = [Link]['publication_date']
genre = [Link]['genre']
[Link](title=title, author=author, publication_date=publication_date,
genre=genre)
return redirect('book_list')
return render(request, 'books/book_add.html')
def book_search(request):
query = [Link]('q', '')
books = [Link](title__icontains=query) |
[Link](author__icontains=query)
return render(request, 'books/book_search.html', {'books': books, 'query': query})
def book_delete(request, book_id):
book = get_object_or_404(Book, id=book_id)
[Link]()
return redirect('book_list')
5. Création des templates
Créez un dossier templates/books/ et ajoutez les fichiers suivants :
1. book_list.html :
<h1>Liste des livres</h1>
<a href="{% url 'book_add' %}">Ajouter un livre</a>
<form method="get" action="{% url 'book_search' %}">
<input type="text" name="q" placeholder="Rechercher un livre">
<button type="submit">Rechercher</button>
</form>
<ul>
{% for book in books %}
<li>{{ [Link] }} par {{ [Link] }}
<a href="{% url 'book_delete' [Link] %}">Supprimer</a>
</li>
{% endfor %}
</ul>
2. book_add.html :
<h1>Ajouter un livre</h1>
<form method="post">
{% csrf_token %}
<input type="text" name="title" placeholder="Titre">
<input type="text" name="author" placeholder="Auteur">
<input type="date" name="publication_date" placeholder="Date de publication">
<input type="text" name="genre" placeholder="Genre">
<button type="submit">Ajouter</button>
</form>
3. book_search.html :
<h1>Résultats de la recherche</h1>
<form method="get" action="{% url 'book_search' %}">
<input type="text" name="q" value="{{ query }}" placeholder="Rechercher un livre">
<button type="submit">Rechercher</button>
</form>
<ul>
{% for book in books %}
<li>{{ [Link] }} par {{ [Link] }}</li>
{% endfor %}
</ul>
6. Configuration des URLs
Dans books/[Link] :
from [Link] import path
from . import views
urlpatterns = [
path('', views.book_list, name='book_list'),
path('add/', views.book_add, name='book_add'),
path('search/', views.book_search, name='book_search'),
path('delete/<int:book_id>/', views.book_delete, name='book_delete'),
]
*Dans LibraryManagement/[Link], incluez les URLs de l'application :
from [Link] import admin
from [Link] import path, include
urlpatterns = [
path('admin/', [Link]),
path('books/', include('[Link]')),
]
7. Lancer le projet
1. Démarrez le serveur Django :
python [Link] runserver
Accédez à l’application via l’URL :
Liste des livres : [Link]
Ajouter un livre : [Link]
Rechercher un livre : [Link]