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

EndpointMéthodeAuthDescription
/api/auth/register/POSTNonInscription
/api/auth/login/POSTNonObtenir token
/api/posts/GETNonLister posts publiés
/api/posts/POSTTokenCréer un post
/api/posts/{id}/publish/POSTTokenPublier
/api/posts/{id}/comments/GET/POSTMixteCommentaires

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"
← Retour au cours 🧠 QCM Module 07 Module 08 — Déploiement →