Styling w/ CSS - Django Web Development with Python p.5
Based on sentdex's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Materialize CSS is integrated by loading its CSS and required JavaScript assets so UI components render with the framework’s styling and behaviors.
Briefing
Materialize CSS is used to give the Django tutorial site a polished, responsive look without hand-writing large amounts of CSS. Instead of wrestling with layout, typography, and UI styling from scratch, the project pulls in Materialize’s CSS and JavaScript assets so components like cards and navigation bars adopt “material design” styling—visually layered elements that behave consistently across screen sizes.
The workflow starts like adding any custom stylesheet: include the framework’s CSS (and the required JavaScript) in the template so the browser loads the styling immediately. After refreshing, the site’s typography changes from the default, while the overall structure remains the same—confirming the framework is active. The tutorial then points to Materialize’s built-in helpers, especially the grid system, which assumes a 12-column layout per row. By assigning column spans per breakpoint (small, medium, large), content can shift from side-by-side layouts on wider screens to stacked layouts on smaller devices.
To apply the grid, the homepage content is reorganized using a `div class="row"` wrapper and Materialize “cards” copied into the loop that iterates over tutorials. Each tutorial card is configured to take up different column widths depending on device size—full width on small screens, half width on medium screens, and a smaller fraction on large screens—so multiple tutorial entries can display in a responsive grid. The card content is populated with fields like the tutorial title and published information, and the card action area is adjusted (removed for now, with the option to add a call-to-action later).
Next comes the navigation bar. A Materialize navbar template is copied into the base page structure and its links are wired to Django routes (e.g., home, register, login). The navbar is treated as shared layout content, which leads to a cleaner Django templating approach: the homepage is refactored to use template inheritance. A new base template (named `header.html` in the tutorial) wraps common elements and defines a `block content` region. The `home.html` page then uses `{% extends %}` and fills only the unique homepage markup inside that block. This avoids copying the navbar and layout across multiple pages later.
Finally, the tutorial shifts toward customizing Materialize’s look via Sass rather than editing thousands of lines of compiled CSS. The process involves installing a Sass compiler (the tutorial uses Koala Sass), editing Materialize’s Sass variables—particularly color variables in the `color` component—then compiling to generate a new CSS file. The compiled CSS is placed into the project’s static directory and included by the base template so the updated theme (e.g., swapping Materialize’s default red palette for a chosen blue palette) propagates to UI elements like the navbar and buttons.
With styling largely handled through Materialize and template inheritance, the next step is to move away from design work and focus on Django user accounts: registration, login, and logout.
Cornell Notes
Materialize CSS is integrated into a Django project to speed up UI styling and ensure responsive layouts. The grid system (12 columns with breakpoint-based column spans) lets tutorial cards shift from multi-column layouts on large screens to stacked layouts on mobile. A Materialize navbar is added and then centralized using Django template inheritance: a shared base template wraps common layout and exposes a `block content` for page-specific markup. Theme customization is done through Sass variables (edited in Materialize’s source color settings) and then compiled into a new CSS file using a Sass compiler. This combination reduces repetitive HTML/CSS work and makes future page additions cleaner.
Why include both Materialize CSS and JavaScript, and what changes after refreshing?
How does Materialize’s grid system enable responsive tutorial card layouts?
How does the tutorial refactor the homepage to avoid repeating the navbar across pages?
What Materialize components are highlighted as useful for a Django app UI?
How is Materialize theming customized without editing compiled CSS line-by-line?
Review Questions
- What is the practical benefit of using Django `{% extends %}` and `{% block %}` when adding new pages like tutorials, about, or forum pages?
- How would you configure a tutorial card to show 3 cards per row on large screens using a 12-column grid?
- Why does changing Sass variables require recompiling into a new CSS file before the browser reflects the theme update?
Key Points
- 1
Materialize CSS is integrated by loading its CSS and required JavaScript assets so UI components render with the framework’s styling and behaviors.
- 2
The 12-column grid system enables responsive layouts by assigning different column spans at small, medium, and large breakpoints.
- 3
Tutorial cards are placed inside a `row` and populated within a Django loop, with card columns configured to reflow across device sizes.
- 4
A Materialize navbar is wired to Django routes (home, register, login) and then centralized using template inheritance to prevent duplication.
- 5
Django template inheritance is implemented by creating a shared base template with a `block content` region and extending it from `home.html`.
- 6
Theme customization is performed by editing Materialize Sass color variables and compiling to a new CSS file, rather than editing the generated CSS directly.
- 7
Compiled CSS must be correctly placed in the Django static directory and included by templates so the browser can load the updated theme.