UNC BACS 350

Web Apps with Python/Django

Logo

Lesson 13 - View Inheritance

LEARN

Week 5 - Data

Project 4

Project 5

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

Development Workflow

Create a Base Theme

Use Theme

Collecting the Static Files

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'