9.5. Views Detail
9.5.1. Function Based Views
>>> from django.shortcuts import render
>>> from myapp.models import Product
>>>
>>>
>>> def product_list(request):
... products = Product.objects.all()
... return render(request, 'myapp/product-list.html', {
... 'products': products,
... })
9.5.2. Class Based Views
>>> #
... from django.views.generic import ListView
... from myapp.models import Product
...
...
... class ProductList(ListView):
... model = Product
... template_name = 'myapp/product-list.html'
... context_object_name = 'products'
9.5.3. Use Case - 1
>>> #
... from django.http import Http404
... from django.shortcuts import render
... from shop.models import Product
...
...
... def product_detail(request, product_id):
... try:
... p = Product.objects.get(pk=product_id)
... except Product.DoesNotExist:
... raise Http404("Product does not exist")
... return render(request, 'shop/product-detail.html', {'product': p})
9.5.4. Assignments
# FIXME: Write tests
# %% About
# - Name: Django View FBV
# - Difficulty: easy
# - Lines: 6
# - Minutes: 5
# %% 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 function based view named: `product_details_view()`
# 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.shortcuts.render` to render template
# 5. Create `shop/product-detail.html` template
# 6. Register view in `urls.py` with name `shop-product-details-fbv`
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Stwórz widok oparty o funkcję o nazwie: `product_details_view()`
# 2. Widok ma zwrócić HTML z detalami produktu
# 3. Primary Key produktu zostanie przekazany w parametrze `pk`
# 4. Użyj `django.shortcuts.render` do renderowania szablonu
# 5. Stwórz szablon `shop/product-detail.html`
# 6. Zarejestruj widok w `urls.py` z nazwą `shop-product-details-fbv`
# %% 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 View CBV
# - Difficulty: easy
# - Lines: 6
# - Minutes: 5
# %% 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 class based view named: `ProductDetailsView()`
# 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ę o nazwie: `ProductDetailsView()`
# 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`
# %% 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