This is a to-do list app. This application uses: django rest framework to create api and jwt to create token authentication system.
- Clone project and create virtualenv
git clone https://github.com/vanya2143/todos.git
cd todos
pip3 install virtualenv
python3 -m venv .env
- Activate virtualenv, install requirements and runserver
source .env/bin/activate
pip install -r requirements.txt
python manage.py migrate
python manage.py runserver
Use curl utility in code examples
curl --request POST \
--header "Content-Type: application/json" \
--data '{"username":"user","password":"secret_password"}' \
http://localhost:8000/api/auth/register/
Response:
{"username":"user"}
curl --request POST \
--header "Content-Type: application/json" \
--data '{"username":"user","password":"secret_password"}' \
http://localhost:8000/api/auth/token/
Response:
{
"refresh":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTYxNTU3MTgwMiwianRpIjoiMzhjODE0Njc1MWEyNDI2OGE1NGI0ZDI1OTJlNTdiM2QiLCJ1c2VyX2lkIjo0fQ.YqHPS57ez1RZkADAqo3VZTWe3ubU9ooGVtUukqlePPI",
"access":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjE1NDg3MjAyLCJqdGkiOiI4Yzk0MDhhNzZjNDU0MzM5ODYyNWQyZjc2YTYxNzdiYiIsInVzZXJfaWQiOjR9.BtIimdMKnDWN7DkeBtCfXbLqJwoDnpO_VeGlLOaRFHA"
}
We store the access token in a variable for a shorter string length
TOKEN="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjE1NDg3MjAyLCJqdGkiOiI4Yzk0MDhhNzZjNDU0MzM5ODYyNWQyZjc2YTYxNzdiYiIsInVzZXJfaWQiOjR9.BtIimdMKnDWN7DkeBtCfXbLqJwoDnpO_VeGlLOaRFHA"
Writable fields:
{
"title": "Todo title",
"body": "Todo body!",
"done": false # true - If you have done this todo
}
Request:
We used an access token from get-jwt-token
curl --request POST \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${TOKEN}" \
--data '{"title":"My First Todo","body":"Todo body"}' \
http://localhost:8000/api/todos/
Response:
{
"id":2,
"title":"My First Todo",
"body":"Todo body",
"done":false,
"owner":"user",
"url":"http://localhost:8000/api/todos/2",
"created":"2021-03-11T18:01:32.599423Z"
}
curl --request GET \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${TOKEN}" \
http://localhost:8000/api/todos/
Reaponse:
[
{
"id":2,
"title":"My First Todo",
"body":"Todo body",
"done":false,
"owner":"user",
"url":"http://localhost:8000/api/todos/2",
"created":"2021-03-11T18:01:32.599423Z"
}
]
Now we have completed the task and want change "done" field.
The url has a task id http://localhost:8000/api/todos/
+ id
curl --request PUT \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${TOKEN}" \
--data '{"title":"My First Todo","body":"Todo body", "done": "true"}' \
http://localhost:8000/api/todos/2 # 2 it's task id
Response: So, we see that the "done" field has changed.
{
"id":2,
"title":"My First Todo",
"body":"Todo body",
"done":true,
"owner":"user",
"url":"http://localhost:8000/api/todos/2",
"created":"2021-03-11T18:01:32.599423Z"
}
curl --request DELETE \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${TOKEN}" \
http://localhost:8000/api/todos/2
Response:
HTTP/1.1 204 No Content
We can list all users with their tasks. We need administrator rights for this request.
curl --request GET \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${TOKEN}" \
http://localhost:8000/api/users/
Response:
{
"count":3,
"next":null,
"previous":null,
"results":
[
{
"id":1,
"username":"admin",
"url":"http://localhost:8000/api/users/1",
"todos": []
},
{
"id":4,
"username":"user",
"url":"http://localhost:8000/api/users/4",
"todos":
[
"http://localhost:8000/api/todos/4"
]
}
]
}