How to Use Bootstrap in a Django Project

Since its release in 2013, Bootstrap has revolutionized how developers style web pages. Bootstrap is a popular front-end framework used to design responsive web applications.

Django uses Bootstrap’s pre-built CSS styles and JavaScript plugins to style web pages. While it reduces the hassle of styling, configuring it in Django can be challenging.

4

Let’s learn how to install and configure Bootstrap in a Django application.

How to Install Bootstrap

There are two ways touse Bootstrap 5in a Django project. You can install it in your application or reference the files usingBootstrap’s CDN. This project will use the former method.

Before installing Bootstrap,create a Django projectand an application called gallery. The application will be a photo gallery, and you’ll use Bootstrap to style the application’s navigation bar.

A dark monitor displaying html code with bootstrap utility classes

Next, install Bootstrap with the following command:

Check the Pipfile and confirm there is a Bootstrap 5 dependency. You now need to notify the Django project that you are using a Bootstrap dependency.

styled navigation bar on browser

In thesettings.pyfile, register Bootstrap as shown below:

Registering Bootstrap in the project settings connects the django-bootstrap5 dependency to your project. It will be available to any other application defined in the project.

Picture of a screen with coding

Apply Bootstrap on a Template

First, create a folder namedtemplatesin your application folder. Inside this folder, create abase.htmlfile and anavbar.htmlfile. The templates will contain HTML files that Bootstrap will style.

While you can apply Bootstrap on thenavbar.htmltemplate, using a base file is conventional. Abase.htmlfile will contain all the scripts and links to apply to any page. It reduces the complexity of individual templates, making your code cleaner and easier to understand.

mega cloud storage with mega logo on laptop screen.

In thebase.htmlfile, include the following:

First, load the bootstrap5 dependency. Then create two block styles where you will define all the styles for the templates. Include the{% bootstrap_css %}template tag and a link to the Bootstrap CSS file.

Next, create a block script that defines the JavaScript functionality.

Including thenavbar.htmlin thebase.htmllinks it to Bootstrap.

Test the configuration by adding Bootstrap styles to thenavbar.htmltemplate:

Now, run the server and check your site in a browser. You should see the styling that Bootstrap applies to the navigation bar.

Why Use Bootstrap in Django Projects?

You’ll mostly use Django for back-end development, but it can make amazing front-end pages too. Using Bootstrap to style front-end pages is good practice for Django beginners. You get an in-depth understanding of Django when you create full-stack applications.

Like any front-end framework, you can use Bootstrap classes with a Django project to style your web pages. Bootstrap 5 has better components and a fast style sheet. It also has improved responsiveness for modern devices.

Why not use Bootstrap to style and create amazing UIs for your Django projects? Django will amaze you with its capabilities in web development.

Bootstrap has several features and can help you bring several ideas to life. Here’s a full beginner’s guide to help you use it efficiently.

OneDrive is one of the best, but it has a catch.

It’s not super flashy, but it can help to keep your computer up and running.

I found my TV was always listening—so I shut it down.

These films will leave you questioning humanity, but also wanting more.

confirm you don’t miss these movies and shows before Netflix removes them.

Technology Explained

PC & Mobile