4 out of the 9 HTTP methods:
HTTP/1.1 200 OK
Date: Sat, 09 Oct 2010 14:28:02 GMT
Server: Apache
Last-Modified: Tue, 01 Dec 2009 20:18:22 GMT
ETag: "51142bc1-7449-479b075b2891b"
Accept-Ranges: bytes
Content-Length: 29769
Content-Type: text/html
<!DOCTYPE html...
Additional key-value pairs passed with a request or response.
HTTP status ranges in a nutshell:
— Steve Losh (@stevelosh) August 28, 2013
1xx: hold on
2xx: here you go
3xx: go away
4xx: you fucked up
5xx: I fucked up
Read more: What is an URL?, MDN
4 of the 6 principles:
Beginners can assume a REST API means an HTTP service that can be called using standard web libraries and tools.
mkdir webservice
cd webservice
python3 -m venv env
env/bin/python --version
env/bin/pip list
env/bin/pip install django
Scripts
instead of bin
and might to use backslashes and/or python
or py
:mkdir webservice
cd webservice
py -m venv env
env\Scripts\python --version
env\Scripts\pip list
env\Scripts\pip install django
env/bin/django-admin --help
env/bin/django-admin startproject --help
env/bin/django-admin startproject webservice .
env/bin/django-admin startapp shop
env/bin/python manage.py --help
env/bin/python manage.py migrate
env/bin/python manage.py createsuperuser
env/bin/python manage.py runserver
from django.http import JsonResponse
def view_article(request):
return JsonResponse({
'id': 1,
'name': 'Screwdriver'
})
from django.urls import path
from shop.views import view_article
urlpatterns = [
path('articles/1/', view_article),
]
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls')),
]
urlpatterns = [
path('articles/<int:id>/', view_article),
]
def view_article(request, id):
return JsonResponse({
'id': id,
'name': 'Screwdriver'
})
def view_article(request, id):
if request.method == 'GET':
pass
from json import loads
def view_article(request, id):
data = loads(request.body)
from django.http import HttpResponseNotAllowed
def view_article(request, id):
return HttpResponseNotAllowed(['GET', 'POST'])
fetch("https://api.quotable.io/random")
.then((response) => {
console.log("HTTP Response status: " + response.status);
return response.json();
})
.then((data) => console.log(data));
const data = {
/*... */
};
fetch("/shop/articles", {
method: "POST", // *GET, POST, PUT, DELETE, etc.
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => console.log(data));
Read more: Using Fetch, MDN
shop/models.py
from django.db.models import Model
from django.db.models import CharField
class Article(Model):
name = CharField(max_length=50)
objects
property of any modelarticle = Article.objects.create(name=name)
article.save()
articles = Article.objects.all()
articles = Article.objects.all()[:5]
articles = Article.objects.order_by('name').all()[:5]
articles = Article.objects.order_by('-name').all()[:5]
None
)article = Article.objects.filter(id=id).get()
article = Article.objects.filter(id=id).first()
article = Article.objects.filter(id=id).get()
article.name = name
article.save()
article = Article.objects.filter(id=id).get()
article.name = name
article.save()
article.delete()
env/bin/python manage.py makemigrations
env/bin/python manage.py migrate
shop/admin.py
from django.contrib.admin import ModelAdmin, register
from shop.models import Article
@register(Article)
class ArticelAdmin(ModelAdmin):
pass
env/bin/pip install -r requirements.txt
django==3.2.4
djangorestframework
env/bin/pip install djangorestframework
INSTALLED_APPS = [
...
'rest_framework',
]
shop/views.py
from shop.models import Article
from shop.serializers import ArticleSerializer
from rest_framework.viewsets import ModelViewSet
class ArticleViewSet(ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
shop/serializers.py
from shop.models import Article
from rest_framework.serializers import HyperlinkedModelSerializer
class ArticleSerializer(HyperlinkedModelSerializer):
class Meta:
model = Article
fields = ['id', 'name']
read_only_fields = ['id']
shop/urls.py
from shop.views import ArticleViewSet
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register('articles', ArticleViewSet)
urlpatterns = router.urls
https://duckduckgo.com/?t=ffab&q=query+parameters&ia=web
env/bin/pip install django-filter
INSTALLED_APPS = [
...
'django_filters',
]
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend'
]
}
shop/filters.py
from django_filters import FilterSet
from shop.models import Article
class ArticleFilter(FilterSet):
class Meta:
model = Article
fields = ['category']
shop/views.py
from shop.filters import ArticleFilter
from shop.models import Article
from shop.serializers import ArticleSerializer
from rest_framework.viewsets import ModelViewSet
class ArticleViewSet(ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
filterset_class = ArticleFilter
settings.py
REST_FRAMEWORK = {
...
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 5
}