Back to Blog

Django HTML CSS Templates: A Beginner’s Guide

by Peter Szalontay, November 13, 2024

Django HTML CSS Templates: A Beginner’s Guide

Django helps build dynamic web applications quickly and with ease using Python. Django templates are at the core of its front-end design, which combines HTML, CSS, and its own templating language to give developers access to beautiful, reusable web pages.

Introduction to Django Templates

Note that the custom HTML and CSS templates are applicable in Django after you have designed your layout according to how you want your project to look. The basic structure includes an app folder with template and static file (CSSs and images) directories. Django’s template system lets you reuse HTML, using template inheritance to keep the code DRY.

Code Example: Basic Django Template Setup

A simple setup of an HTML and CSS template Django project. Create a templates directory inside your Django app folder:

my_app/

templates/

my_app/

base.html

home.html

static/

my_app/

css/

style.css

In the above structure, base. html is the default layout file for your project, home. html will inherit this basic template.

Creating the Base Template

The `base.html` file acts as a blueprint. It typically contains the HTML structure, header, and a CSS link. Below is an example:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>My Django Site</title>

<link rel="stylesheet" href="{% static 'my_app/css/style.css' %}">

</head>

<body>

<header>

<h1>Welcome to My Django Site</h1>

</header>

<main>

{% block content %}{% endblock %}

</main>

</body>

</html>

In this template, {% static %} has been used to include the CSS file. The {% block content %} tag is a placeholder for page-specific content, which will be supplied by templates that extend base. html.

Creating and Styling Content Pages

With our base template ready, let us create our content page, home. html, which makes it inherit from base. html. To do this in Django, we use the {% extends %} and {% block %} tags:

{% extends 'my_app/base.html' %}

{% block content %}

<section>

<p>Hello! This is the home page of our Django site.</p>

</section>

{% endblock %}

By inheriting base.html, the home. html only needs to describe what is unique to that page. This prevents code duplication and keeps your code clean and manageable.

Adding CSS Styling

Add a style to style your Django templates. css file located in the static folder. Take a look at an example of some basic CSS:

/* my_app/static/my_app/css/style.css */

body {

font-family: Arial, sans-serif;

}

header {

background-color: #4CAF50;

color: white;

padding: 10px;

text-align: center;

}

section {

margin: 20px;

}

Now, with this CSS file, your header features its own background color, and the rest of your elements inherit these basic styles. Django is going to render this CSS file on every page that uses base. html.

Production Tip: Whenever I am working with the templates in Django, I have noticed that template syntax errors are usually because of missing the tags or file paths. In addition, your console will provide you with specific errors that help pinpoint the problem to individual parts of your HTML templates.

Personal Experience Note: You might want to have some sort of consistent template structure across your projects, which is something that’s improved my workflow a lot. We try to keep the layout of the HTML and the styling logic clean in order to not end up with a lot of messy code in the future. This has been a real timesaver thanks to template inheritance in Django!

How to Organize HTML Templates in Django

Organizing HTML Template Files in Your ProjectYou can keep your project well-organized by grouping related elements together into folders inside the templates directory and using naming conventions that make it clear what each template does. This facilitates navigating and maintaining the code over time. Creating a clear inside folder hierarchy can help your fellow developers to find specific templates very quickly which can be very useful for bigger projects.

django-html-css-templates

Using CSS in Django to Make a Uniform Look and Feel

When designing with a professional aesthetic in mind, consistency across pages is important. Word of advice: use CSS variables or CSS framework to declare colors, fonts, and spacings uniformly throughout the project. When using Django, you can do this by placing the common CSS in the static folder. Also, a consistent design strengthens your brand and makes for a better user experience.

Template Inheritance for Reusable Layouts

Template inheritance is a powerful way of reusing layouts in Django across multiple pages. This means that you can develop a base template with dynamic sections that each page can inherit and replace the content of. Not only does this save time but it also means that if you change the layout, that change will automatically apply to all pages inheriting from the base template.

CSS in Django: Tricks to Build Responsive Approach

Use responsive design principles in your CSS to keep your templates looking great across all devices. Make sure to adjust that layout and your font sizes for the screen size with media queries. By separating structure (HTML) with styling (CSS), Django templates are making it really easy to create flexible designs, allowing you to test different layouts without affecting the overall design of your templates.

FAQs

Why Use Template Inheritance in Django?

It keeps your code organized and prevents you from repeating yourself. Creating a base template that contains all of your shared layout elements means that most of your templates only need to provide page-specific content, resulting in faster development.

How Does Django Handle Static Files in Production?

Django does not serve static files automatically in production. But you can host them on something like Amazon S3 (or even configure NGINX to serve them). Before deployment, remember to run collectstatic so that your files are collected.

Final Thoughts

Django provides a templating system for HTML and CSS which makes web development quite simpler. After getting started with the basics of template inheritance and static file management, you’ll find Django to be an efficient and productive framework. Experiment with a few pages of your own, just to get the feel for it, and soon you’ll be witnessing the power of Django templating in action!

Automate Your Business with AI

Enterprise-grade AI agents customized for your needs

Discover Lazy AI for Business

Recent blog posts