Full-stack demo consisting of a Django REST API (DRF + SimpleJWT + Algolia + CORS) with two example clients:
- Web client: vanilla HTML/JS using Algolia InstantSearch
- Python client: scripted JWT login + paginated product listing
- Backend: Django 5, Django REST Framework, SimpleJWT,
django-cors-headers,algoliasearch-django, SQLite (dev) - Clients: Vanilla JS + InstantSearch, Python
requests - Env:
python-dotenv
backend/
django_project/
manage.py
django_project/
settings.py
urls.py
routers.py
api/
products/
search/
client/
index.html
client.js
py_client/
jwt.py
requirements.txt
- Python 3.10+
- Pip
# 1) Create & activate a virtualenv (recommended)
python -m venv .venv
source .venv/bin/activate
# 2) Install dependencies
pip install -r requirements.txt
# 3) Configure environment variables (create file below)
cat > backend/django_project/.env << 'EOF'
SECRET_KEY=change_me_dev_secret
APPLICATION_ID=your_algolia_app_id
API_KEY=your_algolia_api_key
EOFNotes:
settings.pyusesload_dotenv(); runningmanage.pyfrombackend/django_project/will load.envin that folder.- Default DB is SQLite; see
DATABASESinsettings.pyto use Postgres/etc.
cd backend/django_project
python manage.py migrate
python manage.py runserver 0.0.0.0:8000cd client
python -m http.server 8111
# Open http://localhost:8111cd py_client
python jwt.py
# Prompts for username/password, stores tokens in py_client/creds.json, then lists products- Authorization header type:
Bearer - Access token lifetime: 30 minutes
- Refresh token lifetime: 30 minutes
JWT endpoints (note the double api segment is intentional based on routing):
- Obtain:
POST /api/api/token/ - Refresh:
POST /api/api/token/refresh/ - Verify:
POST /api/api/token/verify/
- Base:
GET /api/(health/sample) - Auth token (DRF token auth):
POST /api/auth/ - Products (class-based mixin view):
GET /api/products/(paginated list;limit&offsetsupported)POST /api/products/GET /api/products/<id>/POST /api/products/<id>/update/POST /api/products/<id>/delete/
- Search:
GET /api/search/?q=<term>
- v2 router (DRF ViewSet):
GET /api/v2/products-abc/GET /api/v2/products-abc/<id>/
During development, http://localhost:8111 and https://localhost:8111 are allowed origins in settings.py. Serve the web client on port 8111 as shown above.
- Set
APPLICATION_IDandAPI_KEYin.env. algoliasearch-djangointegrates with theproductsapp; savingProductinstances will (by default) sync to Algolia based on the integration settings.
- Base API:
http://localhost:8000/api - Prompts for credentials, obtains SimpleJWT tokens, writes
creds.json, and performs authenticated product listing with pagination. - To reset auth, delete
py_client/creds.jsonor choose a new username/password.
- DB: SQLite by default (
db.sqlite3in the Django project folder) - Static files: served via
STATIC_URL = 'static/'in development
- 401/403 from API: ensure Authorization header is
Bearer <access_token>; login again if expired. - CORS blocked in browser: ensure the web client runs on port 8111 and the backend is on 8000.
- Algolia errors: verify
.envAPPLICATION_ID/API_KEYand that theproductsapp is installed.
This project was inspired by the CodingEntrepreneurs DRF tutorial series.