Python - How To Apply Bootstrap On Django

In this tutorial we will try to Apply Bootstrap To Django Framework to make a stunning website. Django is a free and open source web application framework, written in Python. The official project site describes Django as "a high-level Python Web framework that encourages rapid development and clean, pragmatic design. And It’s free and open source to every developer in the world." Getting started First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. Then download the bootstrap front-end framework, here's the link for the Bootstrap http://getbootstrap.com/ After Python IDLE's is installed, open the command prompt then type "pip install Django", and hit enter. tut1 Wait for the django to be downloaded and installed at the same time. Then After that type "python -m django version" to check if django is installed and what version of django is. tut2Creating the App After setting django we will now create the web app for the web server. First create a new folder named "Python - How To Apply Bootstrap On Django", then cd to a newly created folder, then type "django-admin startproject webserver". A new folder will be created on the directory named 'webserver'. tut3Running The Server After creating a project, cd to the newly created directory, then type "manage.py runserver" to start the server running. The "manage.py" is a command of django-admin that utilize the administrative tasks of python web framework. Here is the image of python web server: tut4 Note: Type '127.0.0.1:8000' in the url browser to view the server. When there is code changes in the server just (ctrl + C) to command prompt to stop the server from running, then start again to avoid errors. Creating The Website This time will now create the web app to display the web models. First locate the directory of the app via command prompt cd, then type "manage.py startapp bootstrap". A new directory will be create inside the app named "bootstrap". tut5Setting up The URL This time will now create a url address to connect the app from the server. First Go to webserver directory, then open urls via Python IDLE's or any text editor. Then import "include" module beside the url module. After that copy/paste the code below inside the urlpatterns.
  1. url(r'^bootstrap/', include('bootstrap.urls')),
It will be look like this.
  1. from django.conf.urls import include, url
  2. from django.contrib import admin
  3.  
  4. urlpatterns = [
  5. url(r'^admin/', admin.site.urls),
  6. url(r'^bootstrap/', include('bootstrap.urls')),
  7. ]
Creating The Path For The Pages Now that we set the connect we will now create a path for the web pages. All you have to do first is to go to bootstrap directory, then copy/paste the code below and save it inside bootstrap directory namely 'urls.py' The file name must be urls.py or else there will be an error in the code.
  1. from django.conf.urls import url
  2.  
  3. from . import views
  4.  
  5. app_name = "bootstrap"
  6.  
  7. urlpatterns = [
  8. url(r'^$', views.index, name = "index"),
  9. url(r'^mission$', views.mission, name = "mission"),
  10. url(r'^vision$', views.vision, name = "vision"),
  11. url(r'^aboutus$', views.aboutus, name = "aboutus"),
  12. ]
Creating The Views The views contains the interface of the website. This is where you assign the html code for rendering it to django framework. To do that first open the views.py, the copy/paste the code below.
  1. from django.shortcuts import render
  2.  
  3.  
  4. # Create your views here.
  5.  
  6. def index(request):
  7. return render(request, 'bootstrap/index.html')
  8.  
  9. def mission(request):
  10. return render(request, 'bootstrap/mission.html')
  11.  
  12. def vision(request):
  13. return render(request, 'bootstrap/vision.html')
  14.  
  15. def aboutus(request):
  16. return render(request, 'bootstrap/aboutus.html')
Importing The Bootstrap We will now import the bootstrap framework. All you have to do is to go to bootstrap directory, then create a directory called 'static' and after that create a sub directory of it namely 'bootstrap'. After the directory is created import the css folder of bootstrap inside bootstrap directory sub directory of static. Creating The Mark up Language Now we will create the html interface for the django framework. First go to bootstrap directory, then create a directory called "templates" and create a sub directory on it called bootstrap. base.html
  1. <!DOCTYPE html>
  2. <html lang = "eng">
  3. <head>
  4. <meta charset = "UTF-8" name = "viewport" content = "device=width-device, initial-scale=1" />
  5. {% load staticfiles %}
  6. <link rel = "stylesheet" type = "text/css" href = "{% static 'bootstrap/css/bootstrap.css' %}"/>
  7. </head>
  8. <nav class = "navbar navbar-inverse">
  9. <div class = "container-fluid">
  10. <a class = "navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13.  
  14. {% block body %}
  15. {% endblock %}
  16. </body>
  17. </html>
Save it as "base.html" inside the bootstrap directory sub directory of templates. index.html
  1. {% extends 'bootstrap/base.html' %}
  2. {% block body %}
  3.  
  4. <nav class="navbar navbar-default">
  5. <div class="container-fluid">
  6. <div class="navbar-header">
  7. <ul class="nav navbar-nav">
  8. <li class="active"><a href="#">Home</a></li>
  9. <li><a href="{% url 'bootstrap:mission' %}">Mission</a></li>
  10. <li><a href="{% url 'bootstrap:vision' %}">Vision</a></li>
  11. <li><a href="{% url 'bootstrap:aboutus' %}">About us</a></li>
  12. </ul>
  13. </div>
  14. </nav>
  15.  
  16. <div class="container">
  17. <div class="jumbotron">
  18. <h1>Django Tutorial</h1>
  19. <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
  20. </div>
  21. </div>
  22. {% endblock %}
Save it as "index.html" inside the bootstrap directory "sub directory of templates". mission.html
  1. {% extends 'bootstrap/base.html' %}
  2. {% block body %}
  3.  
  4. <nav class="navbar navbar-default">
  5. <div class="container-fluid">
  6. <div class="navbar-header">
  7. <ul class="nav navbar-nav">
  8. <li><a href="{% url 'bootstrap:index' %}">Home</a></li>
  9. <li class="active"><a href="{% url 'bootstrap:mission' %}">Mission</a></li>
  10. <li><a href="{% url 'bootstrap:vision' %}">Vision</a></li>
  11. <li><a href="{% url 'bootstrap:aboutus' %}">About us</a></li>
  12. </ul>
  13. </div>
  14. </nav>
  15.  
  16. <div class="container">
  17. <div class="jumbotron">
  18. <h1>Mission</h1>
  19. <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
  20. </div>
  21. </div>
  22. {% endblock %}
Save it as "mission.html" inside the bootstrap directory "sub directory of templates". vision.html
  1. {% extends 'bootstrap/base.html' %}
  2. {% block body %}
  3.  
  4. <nav class="navbar navbar-default">
  5. <div class="container-fluid">
  6. <div class="navbar-header">
  7. <ul class="nav navbar-nav">
  8. <li><a href="{% url 'bootstrap:index' %}">Home</a></li>
  9. <li><a href="{% url 'bootstrap:mission' %}">Mission</a></li>
  10. <li class="active"><a href="{% url 'bootstrap:vision' %}">Vision</a></li>
  11. <li><a href="{% url 'bootstrap:aboutus' %}">About us</a></li>
  12. </ul>
  13. </div>
  14. </nav>
  15.  
  16. <div class="container">
  17. <div class="jumbotron">
  18. <h1>Vision</h1>
  19. <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
  20. </div>
  21. </div>
  22. {% endblock %}
Save it as "vision.html" inside the bootstrap directory "sub directory of templates". aboutus.html
  1. {% extends 'bootstrap/base.html' %}
  2. {% block body %}
  3.  
  4. <nav class="navbar navbar-default">
  5. <div class="container-fluid">
  6. <div class="navbar-header">
  7. <ul class="nav navbar-nav">
  8. <li><a href="{% url 'bootstrap:index' %}">Home</a></li>
  9. <li><a href="{% url 'bootstrap:mission' %}">Mission</a></li>
  10. <li><a href="{% url 'bootstrap:vision' %}">Vision</a></li>
  11. <li class="active"><a href="{% url 'bootstrap:aboutus' %}">About us</a></li>
  12. </ul>
  13. </div>
  14. </nav>
  15.  
  16. <div class="container">
  17. <div class="jumbotron">
  18. <h1>About us</h1>
  19. <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
  20. </div>
  21. </div>
  22. {% endblock %}
Save it as "aboutus.html" inside the bootstrap directory "sub directory of templates". Registering The App To The Server Now that we created the inteface we will now then register the app to the server. To do that go to the webserver directory, then open "settings.py" via Python IDLE's or any text editor. Then copy/paste this script inside the INSTALLED_APP variables 'bootstrap.apps.BootstrapConfig'. It will be like this:
  1. INSTALLED_APPS = [
  2. 'bootstrap.apps.BootstrapConfig',
  3. 'django.contrib.admin',
  4. 'django.contrib.auth',
  5. 'django.contrib.contenttypes',
  6. 'django.contrib.sessions',
  7. 'django.contrib.messages',
  8. 'django.contrib.staticfiles',
  9. ]
Migrating The App To The Server Now that we done in setting up all the necessary needed, we will now then migrate the app to the server. To do that open the command prompt then go to the "webserver" directory, then type "manage.py migrate". tut6 Now try to run the server again, and see if all things are done. There you have it we successfully Apply Bootstrap For Django A Web Framework Of Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment