7.3. Views Class Based
Class Based Views (CBV) are a way to define views as classes instead of functions.
7.3.1. Use Case - 1
import json
from http import HTTPStatus
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from contact.models import Customer
@method_decorator(csrf_exempt, name='dispatch')
class ContactAPI(PermissionRequiredMixin, View):
permission_required = ['contact.view_customer']
raise_exception = True
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options']
def head(self, request):
return JsonResponse(status=HTTPStatus.OK, data={})
def options(self, request):
return JsonResponse(status=HTTPStatus.OK, data=self.http_method_names, safe=False)
def get(self, request, pk=None):
data = Customer.objects.all().values()
if pk is not None:
data = data.filter(pk=pk)
if data.exists():
return JsonResponse(
status=HTTPStatus.OK,
data=list(data),
safe=False)
else:
return JsonResponse(
status=HTTPStatus.NOT_FOUND,
data={'details': 'User with this ID does not exist'})
def post(self, request):
try:
data = json.loads(request.body.decode('utf-8'))
firstname = data['firstname']
lastname = data['lastname']
except json.JSONDecodeError as err:
return JsonResponse(
status=HTTPStatus.BAD_REQUEST,
data={'details': f'JSON Decode Error: {err}'})
except KeyError as err:
return JsonResponse(
status=HTTPStatus.NOT_ACCEPTABLE,
data={'details': f'Missing field: {err}'})
except Exception as err:
return JsonResponse(
status=HTTPStatus.INTERNAL_SERVER_ERROR,
data={'details': f'Some other error: {err}'})
else:
Customer.objects.create(
firstname=firstname,
lastname=lastname)
return JsonResponse(
status=HTTPStatus.CREATED,
data={'details': 'created'})
def delete(self, request, pk):
Customer.objects.get(pk=pk).delete()
return JsonResponse(
status=HTTPStatus.ACCEPTED,
data={'details': 'deleted'})
def put(self, request, pk):
try:
data = json.loads(request.body.decode('utf-8'))
firstname = data['firstname']
lastname = data['lastname']
except json.JSONDecodeError as err:
return JsonResponse(
status=HTTPStatus.BAD_REQUEST,
data={'details': f'JSON Decode Error: {err}'})
except KeyError as err:
return JsonResponse(
status=HTTPStatus.NOT_ACCEPTABLE,
data={'details': f'Missing field: {err}'})
except Exception as err:
return JsonResponse(
status=HTTPStatus.INTERNAL_SERVER_ERROR,
data={'details': f'Some other error: {err}'})
else:
Customer.objects.filter(pk=pk).update(
firstname=firstname,
lastname=lastname)
return JsonResponse(
status=HTTPStatus.ACCEPTED,
data={'details': 'updated'})
def patch(self, request, pk):
try:
data = json.loads(request.body.decode('utf-8'))
except json.JSONDecodeError as err:
return JsonResponse(
status=HTTPStatus.BAD_REQUEST,
data={'details': f'JSON Decode Error: {err}'})
except Exception as err:
return JsonResponse(
status=HTTPStatus.INTERNAL_SERVER_ERROR,
data={'details': f'Some other error: {err}'})
else:
Customer = Customer.objects.get(pk=pk)
if firstname := data.get('firstname'):
Customer.firstname = firstname
if lastname := data.get('lastname'):
Customer.lastname = lastname
Customer.save()
return JsonResponse(
status=HTTPStatus.ACCEPTED,
data={'details': 'updated'})
7.3.2. 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 View CBV
# - Difficulty: easy
# - Lines: 6
# - Minutes: 5
# %% English
# 0. Use `myproject.shop`
# 1. Create class based view `ProductList()`
# 2. View must return HTML with list of all products
# 3. Use `django.views.generic.ListView` to render template
# 4. Create `shop/product-list.html` template
# 5. Register view in `urls.py` with name `shop-product-list-cbv`
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Stwórz widok oparty o klasę `ProductList()`
# 2. Widok ma zwrócić HTML z listą wszystkich produktów
# 3. Użyj `django.views.generic.ListView` do renderowania szablonu
# 4. Stwórz szablon `shop/product-list.html`
# 5. Zarejestruj widok w `urls.py` z nazwą `shop-product-list-cbv`
# %% 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 View CBV
# - Difficulty: easy
# - Lines: 6
# - Minutes: 5
# %% English
# 0. Use `myproject.shop`
# 1. Create class based view `ProductDetails()`
# 2. View must return HTML with details of the product
# 3. Primary Key of the product will be passed in `pk` parameter
# 4. Use `django.views.generic.DetailView` to render template
# 5. Create `shop/product-detail.html` template
# 6. Register view in `urls.py` with name `shop-product-details-cbv`
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Stwórz widok oparty o klasę `ProductDetails()`
# 2. Widok ma zwrócić HTML z detalami produktu
# 3. Primary Key produktu zostanie przekazany w parametrze `pk`
# 4. Użyj `django.views.generic.DetailView` do renderowania szablonu
# 5. Stwórz szablon `shop/product-detail.html`
# 6. Zarejestruj widok w `urls.py` z nazwą `shop-product-details-cbv`
# %% 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()
...