Django Base HTML Example

Ann Jul 09, 2026

Django, a high-level Python web framework, is renowned for its rapid development capabilities and clean, pragmatic design. When starting a new Django project, creating a base HTML template is a crucial step. This template serves as a foundation for all your project's pages, ensuring consistency in design and layout. Let's explore a comprehensive Django base HTML example.

Basic CRUD Structure in Django
Basic CRUD Structure in Django

Before diving into the code, ensure you've set up your project and created a new app. In your app's directory, navigate to the 'templates' folder. This is where Django looks for HTML templates by default. If the folder doesn't exist, create it.

the diagram shows how to use django
the diagram shows how to use django

Setting Up the Base Template

In the 'templates' folder, create a new file named 'base.html'. This will be our base template. Django uses a template inheritance mechanism, allowing us to create reusable components and maintain a consistent design across our project.

Understanding Context in Django Templates
Understanding Context in Django Templates

Let's start by defining the basic structure of our 'base.html' file:

```html {% block title %}My Website{% endblock %}

{% block content %} {% endblock %}
```

Understanding Block Tags

a diagram with the words django in different languages
a diagram with the words django in different languages

In the code above, you'll notice the use of '{% block %}' tags. These are Django's template language syntax for defining reusable blocks of content. In our base template, we've defined two blocks: 'title' and 'content'.

Any child template that extends our 'base.html' can override these blocks with their own content. For instance, a child template might look like this:

```html {% extends 'base.html' %} {% block title %}Home{% endblock %} {% block content %}

Welcome to my website!

Customize Views: Add Extra Context in Django
Customize Views: Add Extra Context in Django

This is the home page of my website.

{% endblock %} ```

Including Static Files

Django's template system also allows us to include static files like CSS and JavaScript. To do this, we need to load the static files in our base template and then reference them in our child templates.

🔗 Django Models: OneToOneField (One-to-One Relationship)
🔗 Django Models: OneToOneField (One-to-One Relationship)

In your Django project's 'settings.py' file, ensure that the 'STATIC_URL' and 'STATICFILES_DIRS' settings are correctly configured. Then, in your 'base.html', include the static files:

```html {% block title %}My Website{% endblock %} ```

Navigational Elements

Create Views in Django
Create Views in Django
Django ListView
Django ListView
14 Django Website Template | Online Training Download app from below link
14 Django Website Template | Online Training Download app from below link
Django vs Flask: Which Python Framework Should You Learn First?
Django vs Flask: Which Python Framework Should You Learn First?
Django Insert Data Into Database with Source Code
Django Insert Data Into Database with Source Code
Simple Introduction to Django Forms (Django Tutorial) | Part 45
Simple Introduction to Django Forms (Django Tutorial) | Part 45
Django Views
Django Views
Django Ecommerce with Source Code
Django Ecommerce with Source Code
17 Django Create Dynamic Website Template | Online Training Download app from below link
17 Django Create Dynamic Website Template | Online Training Download app from below link
⚙️ Django Generic Views – Build Faster with Less Code
⚙️ Django Generic Views – Build Faster with Less Code
📄 Template with Model Relations in Django
📄 Template with Model Relations in Django
Django Models – choices Option
Django Models – choices Option
Django: MVT(Model-View-Template)
Django: MVT(Model-View-Template)
DJANGO VS FASTAPI (2026)
DJANGO VS FASTAPI (2026)
Django Models – Field Options Explained
Django Models – Field Options Explained
Start a Django Project
Start a Django Project
Create Django Models
Create Django Models
Basic Attributes of Django Generic Views
Basic Attributes of Django Generic Views
🗄️ Create a Database in Django
🗄️ Create a Database in Django
Django Project Ideas
Django Project Ideas

Our base template should also include navigational elements like a header and navigation menu. This ensures that all pages in our project have a consistent navigation structure.

Let's add a simple header and navigation menu to our 'base.html':

```html

My Website

{% block content %} {% endblock %}
```

Using Django's URL Template Tag

In the navigation menu, we've used Django's 'url' template tag to generate URLs. This ensures that our URLs are dynamic and can be easily managed from our project's URL configuration.

For instance, in our project's 'urls.py', we might have the following URL patterns:

```python from django.urls import path from . import views urlpatterns = [ path('', views.home, name='home'), path('about/', views.about, name='about'), path('contact/', views.contact, name='contact'), ] ```

With these URL patterns, Django can dynamically generate the URLs for our navigation menu.

Including User Authentication

If your Django project requires user authentication, you can include authentication-related elements in your base template. For instance, you might want to display a login/logout link based on whether the user is authenticated or not.

Django provides the 'user' template context processor, which makes the currently logged-in user available in all templates. We can use this to conditionally display login/logout links:

```html

```

In this example, if the user is authenticated, a 'Logout' link is displayed. Otherwise, a 'Login' link is displayed.

With this base template in place, you can now extend it in all your project's pages, ensuring a consistent design and layout across your entire website. Happy coding!