Objectif
Construire une API Blog complète avec Django REST Framework : posts, commentaires, authentification Token, filtres, pagination et admin Django.
Stack : Django 5.0 + DRF 3.15 + django-filter 24.2
Endpoints
| Endpoint | Méthode | Auth | Description |
|---|---|---|---|
/api/auth/register/ | POST | Non | Inscription |
/api/auth/login/ | POST | Non | Obtenir token |
/api/posts/ | GET | Non | Lister posts publiés |
/api/posts/ | POST | Token | Créer un post |
/api/posts/{id}/publish/ | POST | Token | Publier |
/api/posts/{id}/comments/ | GET/POST | Mixte | Commentaires |
Structure
07-django/mini-projet/solution/
├── manage.py
├── taskapi/ ← app principale
│ ├── models.py ← Post, Comment
│ ├── serializers.py
│ ├── views.py ← PostViewSet, CommentViewSet
│ └── urls.py ← router DRF
└── requirements.txt
Setup
cd 07-django/mini-projet/solution
pip install -r requirements.txt
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
# Admin : http://localhost:8000/admin/
# API : http://localhost:8000/api/
# Browsable API : http://localhost:8000/api/posts/
Tests curl
# Inscription
curl -X POST http://localhost:8000/api/auth/register/ \
-H "Content-Type: application/json" \
-d '{"username":"alice","password":"alice123","email":"alice@test.com"}'
# Login → token
TOKEN=$(curl -s -X POST http://localhost:8000/api/auth/login/ \
-d "username=alice&password=alice123" | python3 -c "import sys,json;print(json.load(sys.stdin)['token'])")
# Créer un post
curl -X POST http://localhost:8000/api/posts/ \
-H "Authorization: Token $TOKEN" \
-H "Content-Type: application/json" \
-d '{"title":"Mon premier post Django","content":"Contenu..."}'
# Filtres
curl "http://localhost:8000/api/posts/?status=published&page=1&limit=5"