Lesson 13 - View Inheritance
LEARN
Week 5 - Data
- Lesson 12 - Data CRUD Operations
- Lesson 13 - View Inheritance
- Lesson 14 - Manipulating Data
- Project 5 - Admin Views
- Django for Beginners - Chapter 5 - Blog App
Project 4
- Project 4 requirements do not require a database
- Key Requirements
- List of hero with links
- Details pages for all heros
- Include a photo that is stored on your server
Project 5
- Key Requirements
- Admin views to create database for heroes
- List of hero with links from database
- Details pages for all heros from database
- Include a photo that is stored on your server
- View Inheritance (hero_theme.html)
Theme (base page)
Define a template that can be loaded like any other. Notice the "{% block content %}".
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Book Builder</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
{% block content %}
<h2>No Main Block Defined</h2>
{% endblock content %}
</body>
</html>
Book Pages
The template can be extended to replace the "{% block content %}".
templates/book_list.html
{% extends 'book_theme.html' %}
{% block content %}
<h1>List of Books</h1>
<ul>
{% for book in object_list %}
<li>{{ book.title }} by {{ book.author }}</li>
{% endfor %}
</ul>
{% endblock content %}
templates/book_detail.html
{% extends 'book_theme.html' %}
{% block content %}
<h1>Book Details</h1>
<ul>
<li>Title: {{ object.title }}</li>
<li>Author: {{ object.author }}</li>
</ul>
{% endblock content %}
Views and URLs
book/views.py
from django.views.generic import DetailView, ListView
class BookListView(ListView):
model = book
template_name = 'book_list.html'
class BookDetailView(DetailView):
model = book
template_name = 'book_detail.html'
config/urls.py
from django.urls import path
from book.views import BookDetailView, BookListView
urlpatterns = [
path('', BookListView.as_view()),
path('<int:pk>', BookDetailView.as_view()),
]
A Good Theme
- Build a brand and should control the appearance
- The theme loads both CSS and JavaScript
- A header, footer, menus, and main are defined
- Use blocks to define replaceable content
BUILD
Development Workflow
- Pull
- Change
- Test
- Push
- Deploy
Create a Base Theme
- Build 'templates/hero_theme.html'
- Include the common page elements and stylesheet
- Work on the visual appearance
Use Theme
- Make the "hero_list.html" and "hero_detail.html" extend the "hero_theme.html"
Collecting the Static Files
- Static files must be set up for Admin Views
- Django 'collectstatic' is used to gather files needed
- Admin files will be deployed to Python Anywhere
config/settings.py
# Place to serve static files
STATICFILES_DIRS = [BASE_DIR / "static"]
# Place to collect static files
STATIC_ROOT = BASE_DIR / "static_assets"
Move the files into static directory
$ mv static_assets/admin static/admin
$ rm -rf static_assets
$ git commit -am 'Add Admin static files'