Jul 23, 2024
python3.10 -m venv venv
, source venv/bin/activate
(Mac/Linux), venv\Scripts\activate
(Windows)mkdir -p ~/dev/drf
, cd ~/dev/drf
requirements.txt
includes Django, DRF, PyYAML, Python Requests, Django CORS Headersdjango-admin startproject cfehome .
, python manage.py runserver 8000
http://httpbin.org/anything
python manage.py startapp api
, add api
to INSTALLED_APPS
in settings.py
api/urls.py
and include in main urls.py
JsonResponse
to return data in viewsmodel_to_dict
and DRF's serializers@property
methods for dynamic dataInstall DRF: Add rest_framework
to INSTALLED_APPS
API Views: Use DRF's APIView
and Response
classes; handle methods (GET, POST)
Generic Views: Shorten code using ListCreateAPIView
, RetrieveUpdateDestroyAPIView
Permissions and Authentication
IsAuthenticated
, IsAdminUser
)pip install django-cors-headers
, add to INSTALLED_APPS
and middlewareCORS_ALLOWED_ORIGINS
or use regex in CORS_URLS_REGEX