Django tutorials
mkdir -p django-tute
cd django-tute
python3 -m venv .venv
source .venv/bin/activate
pip install Django
python -m django --version
5.1.1
which django-admin
django-admin startproject aasite
tree aasite
aasite
├── aasite
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
pushd aasite
python manage.py runserver
(CTRL+C)
popd
django-admin startproject absite
pushd absite
python manage.py startapp polls
python manage.py runserver
(CTRL+C)
popd
pushd absite
python manage.py shell
>>> from django.apps import apps
>>> apps.get_app_config('admin').verbose_name
'Administration'
>>> apps.get_app_config('polls').verbose_name
'Polls'
>>> apps.get_app_configs()
>>> exit()
popd
pip install djangorestframework
python -c "import rest_framework; print(rest_framework.VERSION)"
3.15.2
django-admin startproject zzrest
cd zzrest/zzrest
django-admin startapp quickstart
cd ..
python manage.py migrate
python manage.py createsuperuser --email admin@example.com --username admin
python manage.py runserver
curl -H "Accept: application/json; indent=4" -u admin:123456 http://127.0.0.1:8000/users/
curl -s http://127.0.0.1:8000/users/ | jq
open -a Safari http://127.0.0.1:8000
- https://www.django-rest-framework.org/api-guide/permissions/
- https://www.django-rest-framework.org/api-guide/authentication/
- https://www.django-rest-framework.org/topics/documenting-your-api/
django-admin startproject zzrestauth
cd zzrestauth/zzrestauth
django-admin startapp quickstart
cd ..
python manage.py migrate
python manage.py createsuperuser --email admin@example.com --username admin
python manage.py runserver
Auth Token
python manage.py drf_create_token --help
python manage.py drf_create_token admin
Generated token d1ec2c19f942ad9e8a9c836cc4319927cbe59f7e for user admin
curl -s -X GET http://localhost:8000/users/ -H "Authorization: Token d1ec2c19f942ad9e8a9c836cc4319927cbe59f7e" | jq
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://localhost:8000/users/1/",
"username": "admin",
"email": "admin@example.com",
"groups": []
}
]
}
Get token by using username and password
curl -s -X POST -d '{"username": "admin", "password": "123456"}' -H "Content-Type: application/json" "http://localhost:8000/api-token-auth/" | jq
{
"token": "d1ec2c19f942ad9e8a9c836cc4319927cbe59f7e"
}
You also can manage tokens through admin console
API Documentation
pip install drf-spectacular
python manage.py spectacular --help
python manage.py spectacular --format openapi-json | jq
python manage.py spectacular | yq
python manage.py spectacular | yq > openapi.yaml