9.3. Views Template
9.3.1. Function Based Views
>>> from django.shortcuts import render
>>>
>>> def index_view(request):
... firstname = 'Alice'
... lastname = 'Apricot'
... return render(request, 'myapp/index.html', {
... 'firstname': firstname,
... 'lastname': lastname,
... })
9.3.2. Class Based Views
>>> from django.views.generic import TemplateView
>>>
>>> class IndexView(TemplateView):
... template_name = 'myapp/index.html'
...
... def get_context_data(self, **kwargs):
... context = super().get_context_data(**kwargs)
... firstname = 'Alice'
... lastname = 'Apricot'
... return context | {
... 'firstname': firstname,
... 'lastname': lastname,
... }
9.3.3. Assignments
# FIXME: Write tests
# %% About
# - Name: Django View About
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3
# %% 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: `index_view()`
# 2. Create `shop/index.html` template with content: `Hello World`
# 3. Use `django.shortcuts.render` to render template
# 4. Register view in `urls.py` with name `shop-index-fbv`
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Stwórz widok oparty o funkcję o nazwie: `index_view()`
# 2. Stwórz szablon `shop/index.html` z treścią: `Hello World`
# 3. Użyj `django.shortcuts.render` do renderowania szablonu
# 4. Zarejestruj widok w `urls.py` z nazwą `shop-index-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 About
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3
# %% 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: `IndexView`
# 2. Create `shop/index.html` template with content: `Hello World`
# 3. Use `django.views.generic.TemplateView` to render template
# 4. Register view in `urls.py` with name `shop-index-cbv`
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Stwórz widok oparty o klasę o nazwie: `IndexView`
# 2. Stwórz szablon `shop/index.html` z treścią: `Hello World`
# 3. Użyj `django.views.generic.TemplateView` do renderowania szablonu
# 4. Zarejestruj widok w `urls.py` z nazwą `shop-index-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