Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Django IntroductionChapter 4. Create CRUD Web Application

Django Templates with Bootstrap

Django Templates with Bootstrap

Integrating Bootstrap with Django Templates

Bootstrap is one of the most popular front-end toolkits. It provides pre-defined CSS and JavaScript code so that you can quickly implement well-designed web pages.

Bootstrap Quick Start

The following are the key steps to implement the basic Bootstrap template.

1. Go to the bootstrap website (https://getbootstrap.com/) and click on Read the docs button

Bootstrap home page

2. Get code for CDN

Find a CDN version that is easier to implement. Click on the copy button to get the code.

Bootstrap CDN code snippet

3. Paste the code into the base.html file and make some adjustments

Paste the code into the base.html file and adjust the code like shown below. The yellow lines are from Bootstrap. Keep the body contents from the original base.html file and update the website tile to Employee Learning.

templates/base.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Employee Learning</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/....>
</head>
<body>

  <a href="{% url 'course_list'%}">List</a>
  <a href="{% url 'course_create'%}">Create</a>

  {% block body %}
  <p>Insert body contents here</p>
  {% endblock %}

  <h6>2023 Employee Learning</h6>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/.....></script>
</body>
</html>

4. Check the results

Go to the list page. You can see that the style has been slightly adjusted already.

Django List Page with Bootstrap

Add Navbar (Navigation Bar)

Next, add a navigation bar style to the links on the List page and Create page using the Navbar component from Bootstrap.

1. Go to the Navbar section under Components

Check the left sidebar and find the Components section. Under the Components section, you can find the Navbar component.

Bootstrap Navbar

2. Copy the code for one of the Navbar components

There are several sets of code with different sub-components (e.g., dropdown or search bar). You can use the relevant ones for your app. This section will use a simple design without a dropdown or search bar.

Bootstrap Navbar conde snippet

3. Paste the code into the base.html file and make some adjustments

Paste the code into the base.html file (after the <body> tag). Adjust the code like shown below. The white code is from the original code of base.html, and the yellow code is from Bootstrap. Replace the href and the text part with the ones from the original base.html file. Also, add the brand name of this web app "Training Registration".

templates/base.html
<body>

<nav class="navbar navbar-expand-lg bg-body-tertiary">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Training Registration</a>
    <button class="navbar-toggler" type="button" ...>
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link" href="{% url 'course_list'%}">List</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="{% url 'course_create'%}">Create</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

{% block body %}

4. Check the results

Go to the list page. You can see that the navigation bar style has already been adjusted.

Django Navbar with Bootstrap

Also, shrink the browser window size horizontally. As Bootstrap applies the responsive design concept, you can see that the navbar menu items are collapsed under the hamburger menu.

Django Navbar hamburger menu styled with Bootstrap

Style buttons

Bootstrap also provides quick styling code for buttons. You can choose a color of a button by the color theme name such as primary or secondary.

Bootstrap buttons

1. Find code for Bootstrap Buttons

Code for buttons is available under the Button section under Components on the left sidebar.

Bootstrap buttons code snippet

2. Pick a color theme for each button

Before adjusting code, it's better to come up with a design principle first. The following is the guidance for color theme selections.

List page

  • "Go Detail" button: primary

Detail page

  • "Update" button: warning
  • "Delete" button: danger

Create page

  • "Save" button: primary

Delete page

  • "Confirm" button: danger

3. Copy the class code and paste into the tag to style

At this time, you don't need to copy the entire code. Just copy only the class. For example, class="btn btn-primary" for the primary color buttons.

Copy the class code and paste it into the relevant tags. For example, to adjust the Save button on the Create page, add the class with the yellow line below.

templates/course_create.html
<input type="submit" value="Save" class="btn btn-primary">

Do the same for all buttons based on the design principle.

4. Check the results

Go to the Create page. You can see that the button is already nicely styled.

Django Create Page with Bootstrap

Style the List page with a Card and Flex box

On the list page, there are multiple objects. Using Bootstrap, you can quickly make a card-style layout.

1. Find code for Bootstrap Cards

Code for Cards is available in the Cards section under Components on the left sidebar.

Bootstrap Cards

Select one of the card styles and copy the code. In this practice, choose the basic one but delete the image like shown below.

Bootstrap Cards code snippet

2. Paste the code into the course_list.html file and make some adjustments

Paste the code into the list.html file (after the {% for %} tag). Adjust the code like shown below.

templates/course_list.html
{% for course in object_list %}

<div class="card" style="width: 18rem;">
  <div class="card-body">
    <h5 class="card-title">{{ course.title|upper }}</h5>
    <p class="card-text">{{ course.get_level_display }}</p>
    <a href="{% url 'course_detail' course.pk%}" class="btn btn-primary">Go Detail</a>
  </div>
</div>

{% endfor %}

The white code is from the original code of base.html, and the yellow code is from Bootstrap. Basically, adjust the title and text. As we have already styled the button, keep the code for the button as it is.

At this stage, the UI is not really well-design yet.

Django List Page with Bootstrap Card components

3. Adjust the layout and sizes

Usually, adjusting the layout and sizes requires meticulous work. Bootstrap utilities are helpful tools for speeding up layout and size adjustments using short class names.

The code below is an example of layout and size adjustments. The yellow lines add class or style attributes.

templates/course_list.html
{% block body %}

<div class="container text-center">

<h1 class="text-center m-5">LIST Page</h1>

{% for course in object_list %}
<div class="card m-1 d-inline-flex" style="width: 18rem; height:10rem;">
<div class="card-body">
<h5 class="card-title h-50">{{ course.title|upper }}</h5>
<p class="card-text">{{ course.get_level_display }}</p>
<a href="{% url 'course_detail' course.pk%}" class="btn btn-primary p-0 d-block w-50 mx-auto" style="height:1.2rem; font-size:0.8rem">Go Detail</a>
</div>
</div>
{% endfor %}

</div>

{% endblock %}

Here are some explanations of Bootstrap utilities.

  • text-center: Text utility to move content or nested elements to the center horizontally
  • m-#: Spacing utility to adjust margin. You can specify numbers between 0 and 5.
  • d-inline-flex: Display or Flex utility to change the element to flex inline box.
  • h-#: Sizing utility to adjust the height of the element relative to the parent element. 50 means 50% of the parent element's height.
  • p-#: Spacing utility to adjust padding. You can specify numbers between 0 and 5. 0 means zero padding.
  • d-block: Display utility to change the element to a block element.
  • w-#: Sizing utility to adjust the width of the element relative to the parent element. 50 means 50% of the parent element's width.
  • mx-auto: Spacing utility to horizontally center fixed-width block-level content

We are also using the container class to wrap all elements, which sets a max-width at each responsive breakpoint and allows appropriate margins on the right and left edges.

Now, you can see that the layout and sizes are nicely set.

Django List Page with Bootstrap Card components and Flex Box

Style the Detail and Delete page

In this part, we will style the detail page using Bootstrap utilities. The yellow parts in the code below are the updated parts. The text and spacing utilities help you quickly adjust positions and sizes.

templates/course_detail.html
{% block body %}

<div class="container text-center">

  <h1 class="text-center m-5"> DETAIL Page </h1>

  <p class="text-muted mb-0">Course Title</p>
  <h5> {{ object.title|upper }} </h5>

  <p class="text-muted mt-3 mb-0">Level</p>
  <h5>{{ object.get_level_display }}</h5>

  <p class="text-muted mt-3 mb-0">Assigned Employees</p>
  {% for emp in object.employee.all %}
    <p class="mb-0">{{ emp.name }} : {{ emp.division }}</p>
  {% endfor %}

  <a href="{% url 'course_update' object.pk%}" class="btn btn-warning m-3">Update</a>
  <a href="{% url 'course_delete' object.pk%}" class="btn btn-danger m-3">Delete</a>

</div>

{% endblock %}

Also, update the footer in the base.html file with the yellow code below.

templates/base.html
{% endblock %}

<h6 class="text-muted text-center mt-5" style="font-size:0.8rem">2023 Employee Learning</h6>

Now, you can see a better Detail page design.

Django Detail Page with Bootstrap

Next, update the Delete page with the yellow code below.

templates/course_delete.html
{% block body %}

<div class="container text-center">

  <h1 class="text-center m-5"> DELETE Page </h1>

  <form method="post">{% csrf_token %}
    <p>Are you sure you want to delete "{{ object }}"?</p>
    <input type="submit" value="Confirm" class="btn btn-danger">
  </form>

</div>
{% endblock %}
Django Delete Page with Bootstrap

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

How to Add Models to Django Admin

Add Models in Django Admin – admin.py

Developing User Management Functions in Django

User Management Function Development with Django

Building a CRUD Web Application in Django

Chapter 4. Create CRUD Web Application

How to Add Models to Django Admin

Add Models in Django Admin – admin.py

Developing User Management Functions in Django

User Management Function Development with Django

Building a CRUD Web Application in Django

Chapter 4. Create CRUD Web Application

Tags:

CSS

Bootstrap

Django Templates

Django Template Language

Django Introduction
Course Content

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment