{"id":3518,"date":"2020-07-10T11:53:15","date_gmt":"2020-07-10T11:53:15","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3518"},"modified":"2020-07-10T11:53:19","modified_gmt":"2020-07-10T11:53:19","slug":"setup-a-django-3-project-with-bootstrap-4","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2020\/07\/10\/setup-a-django-3-project-with-bootstrap-4\/","title":{"rendered":"Setup a Django 3 project with Bootstrap 4"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>This post looks at how to create a Django 3 project app and incorporate Bootstrap 4 files into it. This also looks at how to setup the folders for putting static assets like images, css files etc. There are different explanations given in various places on the web depending on the version of Django being used.  This post gives the steps on making the above configuration work successfully . This post assumes that you are using a Linux OS but the steps will be applicable to other OS with minor changes. We are using Python3 here.<\/p>\n\n\n\n<p><strong>1.CREATE PROJECT<\/strong><\/p>\n\n\n\n<p>In the folder where you want to create the project type <code>django-admin startproject myproject<\/code> . This will create the project structure. Change current folder to <code>myproject<\/code> . The directory structure should look something like this:<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\">.<br>\n\u251c\u2500\u2500 manage.py<br>\n\u2514\u2500\u2500 myproject<br>\n    \u251c\u2500\u2500 asgi.py<br>\n    \u251c\u2500\u2500 <strong>init<\/strong>.py<br>\n    \u251c\u2500\u2500 settings.py<br>\n    \u251c\u2500\u2500 urls.py<br>\n    \u2514\u2500\u2500 wsgi.py<\/p>\n\n\n\n<p>To test the created site, type <code>python3 manage.py runserver<\/code><\/p>\n\n\n\n<p>On opening http:\/\/localhost:8000 you should see the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"540\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/Django-the-Web-framework-for-perfectionists-with-deadlines.-940x540.png\" alt=\"\" class=\"wp-image-3521\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/Django-the-Web-framework-for-perfectionists-with-deadlines.-940x540.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/Django-the-Web-framework-for-perfectionists-with-deadlines.-620x356.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/Django-the-Web-framework-for-perfectionists-with-deadlines.-300x172.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/Django-the-Web-framework-for-perfectionists-with-deadlines.-768x441.png 768w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/Django-the-Web-framework-for-perfectionists-with-deadlines..png 1256w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>2.CREATE APP<\/strong><\/p>\n\n\n\n<p>We will now create an app within the project. Staying in the project directory (myproject) type<code> python3 manage.py startapp myapp<\/code><\/p>\n\n\n\n<p>The directory structure should be something like this now:<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\">\u251c\u2500\u2500 db.sqlite3<br>\n\u251c\u2500\u2500 manage.py<br>\n\u251c\u2500\u2500 myapp<br>\n\u2502&nbsp;&nbsp; \u251c\u2500\u2500 admin.py<br>\n\u2502&nbsp;&nbsp; \u251c\u2500\u2500 apps.py<br>\n\u2502&nbsp;&nbsp; \u251c\u2500\u2500 <strong>init<\/strong>.py<br>\n\u2502&nbsp;&nbsp; \u251c\u2500\u2500 migrations<br>\n\u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u2514\u2500\u2500 <strong>init<\/strong>.py<br>\n\u2502&nbsp;&nbsp; \u251c\u2500\u2500 models.py<br>\n\u2502&nbsp;&nbsp; \u251c\u2500\u2500 tests.py<br>\n\u2502&nbsp;&nbsp; \u2514\u2500\u2500 views.py<br>\n\u2514\u2500\u2500 myproject<br>\n    \u251c\u2500\u2500 asgi.py<br>\n    \u251c\u2500\u2500 <strong>init<\/strong>.py<br>\n    \u251c\u2500\u2500 <strong>pycache<\/strong><br>\n    \u2502&nbsp;&nbsp; \u251c\u2500\u2500 <strong>init<\/strong>.cpython-36.pyc<br>\n    \u2502&nbsp;&nbsp; \u251c\u2500\u2500 settings.cpython-36.pyc<br>\n    \u2502&nbsp;&nbsp; \u251c\u2500\u2500 urls.cpython-36.pyc<br>\n    \u2502&nbsp;&nbsp; \u2514\u2500\u2500 wsgi.cpython-36.pyc<br>\n    \u251c\u2500\u2500 settings.py<br>\n    \u251c\u2500\u2500 urls.py<br>\n    \u2514\u2500\u2500 wsgi.py<\/p>\n\n\n\n<p><strong>3.CREATE VIEWS<\/strong><\/p>\n\n\n\n<p>We will now create a couple of views for display. Open <code>myapp\/views.py<\/code> and add the following code to it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.shortcuts import render\nfrom django.http import HttpResponse\n\n# Create your views here.\n\ndef index(request):\n    return HttpResponse(\"Home page\")\n\ndef details(request):\n    return HttpResponse(\"Details page\")\n\n\n<\/code><\/pre>\n\n\n\n<p>Create a <code>urls.py<\/code> in the <code>myapp<\/code> folder to handle url redirection. This file will then be called in the <code>urls.py<\/code> within <code>mysite<\/code>. Put the following code into <code>myapp\/urls.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.urls import path\nfrom . import views\n\nurlpatterns = [\n        path('', views.index, name=\"index\"),\n        path('details\/', views.details, name=\"details\"),\n        ]\n\n\n<\/code><\/pre>\n\n\n\n<p>In <code>mysite\/urls.py<\/code> add the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.urls import path\nfrom . import views\n\nurlpatterns = [\n        path('', views.index, name=\"index\"),\n        path('details\/', views.details, name=\"details\"),\n        ]\n\n\n<\/code><\/pre>\n\n\n\n<p><strong>4.SETUP BOOTSTRAP<\/strong><\/p>\n\n\n\n<p>Type <code>pip3 install django-bootstrap4<\/code> to install Bootstrap4 into the django installation<\/p>\n\n\n\n<p>In <code>mysite\/settings.py<\/code>, add<code> bootstrap4<\/code> to the list of <code>INSTALLED_APPS<\/code><\/p>\n\n\n\n<p><strong>5.CREATE TEMPLATES WITH HTML FILES<\/strong><\/p>\n\n\n\n<p>Now we will use HTML files to generate views. First create a folder called <code>templates<\/code> under <code>myapp<\/code> . Create another folder called <code>myapp<\/code> under <code>templates<\/code>. So now the folder structure will be <code>myapp\/templates\/myapp<\/code><\/p>\n\n\n\n<p>We have two views to generate &#8211; index and details. We will create a separate header and footer template which we can use in the other pages. Create another folder <code>components<\/code> under <code>myapp<\/code> . So the folder will be  <code>myapp\/templates\/myapp\/components<\/code><\/p>\n\n\n\n<p>Under components folder create a file <code>header.html<\/code> and add the following script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html>\n&lt;head>&lt;title>&lt;\/title>\n{% load bootstrap4 %}\n{% bootstrap_css %}\n{% bootstrap_javascript jquery='full'%}\n \n&lt;\/head>\n\n<\/code><\/pre>\n\n\n\n<p>Create another file called footer.html and add the following script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;hr>\n&lt;div class=\"row\">\n\t&lt;div class=\"col-sm-12 text-center small\">Footer comes here&lt;\/div>\n&lt;\/div>\n&lt;\/html>\n\n<\/code><\/pre>\n\n\n\n<p>Under <code>templates\/myapp<\/code> folder create index.html and add the following script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{% include \"myapp\/components\/header.html\" %}\n\n&lt;body>\nHomepage\n&lt;div class=\"container\">\n\t&lt;div class=\"row border\">\n\t\t&lt;div class=\"col-sm-3\">Left col\n\t\t&lt;\/div>\n\t\t&lt;div class=\"col-sm-9\">Right col&lt;\/div>\n\t&lt;\/div>\n&lt;div class=\"clearfix\">&lt;\/div>\n&lt;\/div>\n\n{% include \"myapp\/components\/footer.html\" %}\n\n<\/code><\/pre>\n\n\n\n<p>Under <code>templates\/myapp<\/code> folder create details.html and add the following script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{% include \"myapp\/components\/header.html\" %}\n\n&lt;body>\nHomepage\n&lt;div class=\"container\">\n\t&lt;div class=\"row border\">\n\t\t&lt;div class=\"col-sm-12 text-center\">Details page&lt;\/div>\n\t&lt;\/div>\n&lt;div class=\"clearfix\">&lt;\/div>\n&lt;\/div>\n\n{% include \"myapp\/components\/footer.html\" %}\n<\/code><\/pre>\n\n\n\n<p>Now we go back to <code>myapp\/views.py <\/code>and change the code to use templates:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.shortcuts import render\nfrom django.http import HttpResponse\n\n# Create your views here.\n\ndef index(request):\n    return render(request, \"myapp\/index.html\")\n\ndef details(request):\n    return render(request, \"myapp\/details.html\")\n\n<\/code><\/pre>\n\n\n\n<p>We need to add the templates folder to mysite\/settings.py :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nTEMPLATES = [\n    {\n        'BACKEND': 'django.template.backends.django.DjangoTemplates',\n        'DIRS': [\n                '\/var\/www\/myproject\/myapp\/templates',\n            ],\n<\/code><\/pre>\n\n\n\n<p>Run the project again. You should see the following output for http:\/\/localhost:8000\/myapp<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"77\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp--940x77.png\" alt=\"\" class=\"wp-image-3530\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp--940x77.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp--620x51.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp--300x25.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp--768x63.png 768w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-.png 1247w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>And this page for http:\/\/localhost:8000\/myapp\/details<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"83\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-details--940x83.png\" alt=\"\" class=\"wp-image-3531\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-details--940x83.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-details--620x55.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-details--300x26.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-details--768x68.png 768w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-details-.png 1249w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>6.SETUP FOLDERS FOR STATIC ASSETS<\/strong><\/p>\n\n\n\n<p>Now we will setup the folder structure to put up our own assets like images, scripts etc.<\/p>\n\n\n\n<p>Under <code>myproject\/myapp <\/code>create a folder called <code>static<\/code>. Under <code>static<\/code> create three folders called <code>css, img<\/code> and <code>js<\/code>. It should look something like this:<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\">static<br>\n\u251c\u2500\u2500 css<br>\n\u251c\u2500\u2500 img<br>\n\u2514\u2500\u2500 js<\/p>\n\n\n\n<p>Open myproject.settings.py and add the following settings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>STATIC_URL = '\/static\/'\nSTATICFILES_DIRS = [\n    os.path.join(BASE_DIR, \"myapp\/static\"),\n\n]<\/code><\/pre>\n\n\n\n<p>Create a <code>custom.css<\/code> under <code>static\/css<\/code> and add the following script:<\/p>\n\n\n\n<p><code>body {background-color: #eeeeee; font-size:12px; min-height:800px;}<\/code><\/p>\n\n\n\n<p>Create a <code>test.js<\/code> under <code>static\/js<\/code> and add the following script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alert(\"This is javascript\");<\/code><\/pre>\n\n\n\n<p>Add a sample image to <code>static\/img<\/code> folder for testing. We will assume its called <code>python-logo.png<\/code><\/p>\n\n\n\n<p>Open<code> myapp\/header.html <\/code>and update the script as below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html>\n&lt;head>&lt;title>&lt;\/title>\n\t{% load bootstrap4 %}\n{% bootstrap_css %}\n{% bootstrap_javascript jquery='full'%}\n{% load static %}\n&lt;link rel=\"stylesheet\" href=\"{% static \"css\/custom.css\" %}\" \/>\n&lt;script src=\"{% static \"js\/test.js\" %}\">&lt;\/script>\n&lt;\/head>\n<\/code><\/pre>\n\n\n\n<p>Open myapp\/index.html and update the script as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{% include \"polls\/components\/header.html\" %}\n{% load static %}\n\n&lt;body>\nHomepage\n&lt;div class=\"container\">\n\t&lt;div class=\"row border\">\n\t\t&lt;div class=\"col-sm-3\">\n\t\t\t&lt;img src=\"{% static \"img\/python-logo.png\" %}\" class=\"img img-fluid\">\n\t\t&lt;\/div>\n\t\t&lt;div class=\"col-sm-9\">&lt;h1>Right col&lt;\/h1>&lt;\/div>\n\t&lt;\/div>\n&lt;div class=\"clearfix\">&lt;\/div>\n&lt;\/div>\n\n{% include \"polls\/components\/footer.html\" %}\n<\/code><\/pre>\n\n\n\n<p>Before running the app, do a migration with <code>python3 manage.py migrate<\/code><\/p>\n\n\n\n<p>Now run the app with http:\/\/localhost:8000\/myapp . The output should be something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"146\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-1-1-940x146.png\" alt=\"\" class=\"wp-image-3542\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-1-1-940x146.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-1-1-620x96.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-1-1-300x46.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-1-1-768x119.png 768w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2020\/07\/localhost-8000-myapp-1-1.png 1252w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><strong>CONCLUSION<\/strong><\/p>\n\n\n\n<p>The above steps enable your site to use Bootstrap and also put in place your own assets .<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW This post looks at how to create a Django 3 project app and incorporate Bootstrap 4 files into it. This also looks at how <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2020\/07\/10\/setup-a-django-3-project-with-bootstrap-4\/\" title=\"Setup a Django 3 project with Bootstrap 4\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3429,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[297],"tags":[],"class_list":["post-3518","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=3518"}],"version-history":[{"count":25,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3518\/revisions"}],"predecessor-version":[{"id":3547,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3518\/revisions\/3547"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3429"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=3518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}