Django Adding CSS & Bootstrap Styles

This is our fourteenth article in Django, in this article we are going to learn about Django Adding CSS & Bootstrap Styles. so when you are going to develop a website or web application you want to give design for your website, you can use custom CSS Styles or you can use Bootstrap.

 

 

 

Flask Web Development Tutorials

1: Flask CRUD Application with SQLAlchemy 

2: Flask Creating News Web Application 

3: Flask Creating REST API with Marshmallow 

 

 

 

Python GUI Development Tutorials

1: PyQt5 GUI Development Tutorials

2: Pyside2 GUI Development Tutorials

3: wxPython GUI Development Tutorials

4: Kivy GUI Development Tutorials 

5: TKinter GUI Development Tutorials

 

 

 

We need to create a Project in Django, also for django installation you can read this article Django Introduction & Installation .

 

 

 

 

Now you need to create a database in the WAMP Server, so my Database name is codeloop. but you can give what ever you want for the database name.

 

As we have created our Django Project, now we need to open settings.py file in Django Project and bring some changes in the DATABASE section, by default if you see we have SQLite3 database. but we want to use MySQL Database. for this purpose you need to add some configuration of your mysql database, for example like database name,  port, username, password and database host.

 

 

 

 

After adding these configurations in your settings.py file, you need to migrate your project

 

 

Adding CSS Styles 

So for adding CSS Styles you need to create a new directory in your project, the name of the directory should be static, in the static folder you can add your static files like CSS, JavaScript, Bootstrap and Images. after that you need to create a file called style.css, basically we are going to change the color of our h1 and p tag in all our pages.

 

 

 

 

Now we need to create our Django App, i have already talked about Django Apps in details in one of my previous articles, you can check this Django Apps & URL Routing. make sure that you have added your newly created App in the INSTALLED_APP of settings.py

 

 

 

 

So now we need to create our templates, we have already talked about templates in Django, you can read this article Django Templates Introduction. but you need to just create a templates folder in your project and you need to add some html files. also make sure that you have added your templates folder in your settings.py DIRS section.

 

This is our navbar.html, and we have already included this file in to our base.html, you can check this article for How to Include Template Tag in Django  Django Include Template Tag. also we have used url template tag for linking of the views with our navbar, you can read this article for that Django URL Template Tag.

 

 

 

 

OK now we need to link our created CSS in our base.html, and we are going to add the link of that file in our base.html. also at the top you need to add {% load static %}. 

 

 

 

 

Also you need to open your settings.py file and add this code at the end of file, because right now our Django Project does not know about the static folder that we have created, after adding this you notify Django about your static folder.

 

 

 

 

This is our models.py, we have already talked about django models, you can read this article  Django Models Introduction.

 

 

 

 

After adding your model, you need to do migrations like this.

 

 

 

 

This is our views.py file, and we have some views in here.

 

 

 

And these are our html files for the views.

 

 

templates/home.html , in here we have retrieved our data from mysql database, you can read this article Django Rendering Data from Mysql Database.

 

 

 

 

templates/details.html, in here we have retrieved the filtered data, you can read this article Django Retrieving Filtered Data .

 

 

 

 

templates/about.html

 

 

 

 

templates/contact.html

 

 

 

 

Also you need to create your urls, you can read this article for url routing Django Apps & URL Routing . but just create a new python file in your news app at name of urls.py and add these codes. you can see that we have added our new url for details.

 

 

 

 

And also you need to include your app urls.py in your project urls.py file.

 

 

 

 

Now you can run the project and you can see that the color of h1 and p tags are changed in our pages.

 

 

 

 

 

http://localhost:8000/

Django Adding CSS & Bootstrap Styles
Django Adding CSS & Bootstrap Styles

 

 

 

 

Adding Bootstrap To Django Project

Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end web development. It contains CSS- and JavaScript-based design templates for typography, forms, buttons, navigation, and other interface components. there are two ways that you can add Bootstrap in your Django Project, the first way is that you can download Bootstrap from their official website, and add that to your static folder, and you can link that the same as we have done for custom CSS.  the second way is that you can use Bootstrap CDN Links. we are going to use Bootstrap CDN Links.

 

 

 

So now open your base.html and add the links. basically we have added the the CSS Styles with JavaScript.

 

 

 

 

Also we are going to add some styles in our home.html, so now open that html file and add these codes. for example in here for every retrieved data from the database i have added a Bootstrap Card.

 

 

 

 

So now if you go to http://localhost:8000/ , this will be the result.

 

Django Adding CSS & Bootstrap Styles
Django Adding CSS & Bootstrap Styles

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×