16.5. HTTP Methods
GET
POST
PUT
PATCH
DELETE
HEAD
OPTIONS
TRACE
16.5.1. GET vs. POST
?argument1=value&argument2=value
single argument
multiple arguments
arrays
files
multipart
security
16.5.2. POST vs. PUT
16.5.3. POST and CSRF
csrf_token
16.5.4. PATCH?!
16.5.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
16.5.6. Assignments
# FIXME: Write tests
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Django API List
# - Difficulty: easy
# - Lines: 8
# - Minutes: 8
# %% 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`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
...
# FIXME: Write tests
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Django API Get
# - Difficulty: easy
# - Lines: 8
# - Minutes: 8
# %% 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`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
...
# FIXME: Write tests
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Django API Create
# - Difficulty: easy
# - Lines: 8
# - Minutes: 8
# %% 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`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
...