18.6. HTTP Methods — Python
GET
POST
PUT
PATCH
DELETE
HEAD
OPTIONS
TRACE
18.6.1. GET vs. POST
?argument1=value&argument2=valuesingle argument
multiple arguments
arrays
files
multipart
security
18.6.2. POST vs. PUT
18.6.3. POST and CSRF
csrf_token
18.6.4. PATCH?!
18.6.5. OPTIONS and CORS
http_method_names = ['get', 'post', 'options'] def options(self, request, *args, **kwargs): response = HttpResponse(status=200) response['Access-Control-Allow-Origin'] = '*' response['Access-Control-Allow-Methods'] = ', '.join(http_method_names).upper() response['Access-Control-Allow-Headers'] = 'Content-Type' return response
18.6.6. Assignments
# FIXME: Write tests # %% About # - Name: Django API List # - Difficulty: easy # - Lines: 8 # - Minutes: 8 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 0. Use `myproject.shop` # 1. Create an endpoint `GET /api/v1/shop/products` # 2. Endpoint should list all products in a database # 3. You can use view based on class or function # 4. Restrict, that only GET method is handled # 5. Do not use `ninja` # %% Polish # 0. Użyj `myproject.shop` # 1. Stwórz endpoint `GET /api/v1/shop/products` # 2. Endpoint ma wylistować wszystkie produkty w bazie danych # 3. Możesz użyć widoku bazującego na klasach lub na funkcjach # 4. Ogranicz, aby tylko metoda GET była obsługiwana # 5. Nie używaj `ninja` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 12), \ 'Python has an is invalid version; expected: `3.12` or newer.' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' import django; django.setup() from http import HTTPStatus from django.views import View from django.http import JsonResponse from shop.models import Product # %% Types # %% Data # %% Result
# FIXME: Write tests # %% About # - Name: Django API Get # - Difficulty: easy # - Lines: 8 # - Minutes: 8 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 0. Use `myproject.shop` # 1. Create an endpoint `GET /api/v1/shop/product/{pk}` # 2. Endpoint will return product details for given `pk` # 3. Primary Key of the product will be passed in `pk` parameter # 4. You can use view based on class or function # 5. Restrict, that only GET method is handled # 6. Do not use `ninja` # %% Polish # 0. Użyj `myproject.shop` # 1. Stwórz endpoint `GET /api/v1/shop/product/{pk}` # 2. Endpoint na zwróci szczegóły produktu dla danego `pk` # 3. Primary Key produktu zostanie przekazany w parametrze `pk` # 4. Możesz użyć widoku bazującego na klasach lub na funkcjach # 5. Ogranicz, aby tylko metoda GET była obsługiwana # 6. Nie używaj `ninja` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 12), \ 'Python has an is invalid version; expected: `3.12` or newer.' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' import django; django.setup() # %% Types # %% Data # %% Result
# FIXME: Write tests # %% About # - Name: Django API Create # - Difficulty: easy # - Lines: 8 # - Minutes: 8 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 0. Use `myproject.shop` # 1. Create an endpoint `POST /api/v1/shop/product/` # 2. Endpoint will return product details for given `pk` # 3. Input data: # - `name` - product name # - `barcode` - barcode in EAN-13 format # - `price` - product price # 4. You can use view based on class or function # 5. Restrict, that only GET method is handled # 6. Do not use `ninja` # %% Polish # 0. Użyj `myproject.shop` # 1. Stwórz endpoint `POST /api/v1/shop/product/` # 2. Endpoint ma dodawać rekordy do bazy danych # 3. Dane wejściowe: # - `name` - nazwa produktu # - `barcode` - kod kreskowy w formacie EAN-13 # - `price` - cena produktu # 4. Możesz użyć widoku bazującego na klasach lub na funkcjach # 5. Ogranicz, aby tylko metoda GET była obsługiwana # 6. Nie używaj `ninja` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 12), \ 'Python has an is invalid version; expected: `3.12` or newer.' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' import django; django.setup() # %% Types # %% Data # %% Result