9.4. Views List
9.4.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.4.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.4.3. 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_list_view()`
# 2. View must return HTML with list of all products
# 3. Use `django.shortcuts.render` to render template
# 4. Create `shop/product-list.html` template
# 5. Register view in `urls.py` with name `shop-product-list-fbv`
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Stwórz widok oparty o funkcję o nazwie: `product_list_view()`
# 2. Widok ma zwrócić HTML z listą wszystkich produktów
# 3. Użyj `django.shortcuts.render` do renderowania szablonu
# 4. Stwórz szablon `shop/product-list.html`
# 5. Zarejestruj widok w `urls.py` z nazwą `shop-product-list-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: `ProductListView()`
# 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ę o nazwie: `ProductListView()`
# 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`
# %% 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