---
tags: Python Web
---
# Тема 2. Приложение с несколькими страницами
https://github.com/roman-yatsenko/django-topics/tree/main/django-pages
## Начальная настройка
Как и в [теме 1](/@YaRo/python-web-topic01), наша первоначальная настройка включает в себя следующие шаги:
- создание нового каталога для нашего кода
- установить Django в новой виртуальной среде
- создание нового проекта Django
- создание нового приложения pages
- обновление(настройка) settings.py
```shell
pipenv install Django==2.2.16
pipenv shell
django-admin startproject pages_project .
python manage.py startapp pages
```
### pages_project/settings.py
```python=
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages'
]
...
```
```shell
python manage.py runserver
# stop
mkdir templates
touch templates/home.html
```
### pages_project/settings.py
```python=
...
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
},
]
...
```
### templates/home.html
```html=
<h1>Homepage</h1>
```
## Class-Based Views
### pages/views.py
```python=
from django.views.generic import TemplateView
class HomePageView(TemplateView):
template_name = 'home.html'
```
## URLs
### pages_project/urls.py
```python=
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls'))
]
```
```shell
touch pages/urls.py
```
### pages/urls.py
```python=
from django.urls import path
from . import views
urlpatterns = [
path('', views.HomePageView.as_view(), name='home'),
]
```
```shell
python manage.py runserver
# stop
```
## Страница About
```shell
touch templates/about.html
```
### templates/about.html
```html=
<h1>About page</h1>
```
### pages/views.py
```python=
...
class AboutPageView(TemplateView):
template_name = 'about.html'
```
### pages/urls.py
```python=
...
urlpatterns = [
path('', views.HomePageView.as_view(), name='home'),
path('about/', views.AboutPageView.as_view(), name='about'),
]
```
```shell
python manage.py runserver
# stop
```
## Расширение Шаблонов
```shell
touch templates/base.html
```
### templates/base.html
```html
<header>
<a href="{% url 'home' %}">Home</a> | <a href="{% url 'about' %}">About</a>
</header>
{% block content %}
{% endblock %}
```
### templates/home.html
```html=
{% extends 'base.html' %}
{% block content %}
<h1>Homepage</h1>
{% endblock %}
```
### templates/about.html
```html=
{% extends 'base.html' %}
{% block content %}
<h1>About page</h1>
{% endblock %}
```
```shell
python manage.py runserver
# stop
```
## Тесты
### pages/tests.py
```python=
from django.test import SimpleTestCase
class SimpleTests(SimpleTestCase):
def test_home_page_status_code(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
def test_about_page_status_code(self):
response = self.client.get('/about/')
self.assertEqual(response.status_code, 200)
```
```shell
python manage.py test
# stop
```
---
(c) Яценко Р.Н., 2018-2020
[Учебный центр компьютерных технологий "Кит"](http://kit.kh.ua/)
<img src="https://i.imgur.com/Kh901c1.png" style="width: 150px; position: fixed; top: 100px; right: 10px; border: 0; box-shadow: none;">