Tutorial To Do App REST API - django
pip install django
django startproject <nazov>
django startapp <nazov>
- Todoproject -> settings.py -> pridali sme applikaciu
- Vytvorili model Todo - todolist/model.py
pip install rest_framework
- Todoproject -> settings.py -> pridali sme applikaciu "rest_framework"
- Vytvorili serializers.py a v ňom TodoSerializer
- Vytovirli vo views.py - TodoView s napojenim na serializers (TodoSerializer)
- Upravili URLS v urls.py v todoproject.
-> /todoproject/urls.py
from rest_framework import routers
from todolist import views
# Nastavili default router
router = routers.DefaultRouter()
# pridali sme do router TodoView s url /todo
router.register(r'todo', views.TodoView, 'todo')
urlpatterns = [
path('admin/', admin.site.urls),
# všetky router ktoré sú registrované sa vypíšu za /api/*
path('api/', include(router.urls))
]
pip install django-cors-headers
- Todoproject -> settings.py -> pridali sme applikaciu
"corsheaders"
- Todoproject -> settings.py -> Pridali sme middleware
'corsheaders.middleware.CorsMiddleware'
- Todoproject -> settings.py -> Povolili sme všetky domeny
CORS_ALLOW_ALL_ORIGINS = True
- Vytvor aplikáciu 'Recepty od mamičky' s REST API, kde výstup budem JSON.
- JSON:
{ "name": "", "author": "", "thumbnail": "http://url", // obrazok (compres, quality=60, JPEG) "cooking_time": 1, //minuty "portions": 2 "difficulty": "Ľahké" // Ľahké, Stredné, Ťažké "ingredients" : [ { // samostatny model a zapis do databazy na zaklade ID name: "", gram: 10, }, { name: "", gram: 10, },... ], }
- JSON: