Quickstart with Django: Part 1
Python is one of the most popular languages for machine learning and integrating it with web pages can be done with Django. This is a walkthrough in getting your first Django web page working to progress to a path of using ML with your webpages. This is a two part series with this first part involving setting up Django and a simple page and the second part in dealing with multiple pages and data input. VS Code is being used as the IDE.
Assuming python is already installed, use ‘pip’ to install Django:
First, create our project directory and navigate to it. Once we are there, we will use Django to create the necessary files by using ‘django-admin startproject <name of your project>’
Django will create another project folder inside of our project folder and will contain ‘manage.py’ which we will use to run our server.
You can go ahead and navigate to the server at the specific address now to make sure nothing is broken.
Open VS Code and then open our project folder that we created previously when we first created our project directory. In this example, there is a parent folder (project directory) containing the folder that was created by Django. We will open the project directory and will see the following:
It is important to have the parent project folder because we will later create folders alongside the project folder created by Django and not as a child as we will see next.
We will first need to specify a folder to contain your .html files. We will call this ‘template’ to follow with the Django MVC culture/format. Create this directory under the parent project directory as follows:
Once your ‘template’ folder is created. We must tell Django where it is. Open ‘settings.py’.
Navigate to the ‘TEMPLATES’ section and add your ‘templates’ folder to ‘DIRS’
Create our .html file. I simply called it ‘index.html’
Now return to the project directory containing ‘urls.py, settings.py’ and create a file called ‘views.py’ which will link our .html files with functions.
To the empty ‘views.py’ file enter the following:
from django.shortcuts import render
This will remain unchanged for future multiple pages that we can/will add later.
Here we will create a function that will call our ‘index.html’ file. These two lines will be generic for simple .html files until later when we will be dealing with user input data.
Navigate to our ‘url.py’ file and we will need to input the following where ‘from’ refers to the current directory:
from . import views
Finally, add the path under ‘urlpatterns’ as noted:
path(‘’, views.home, name=’home’)
We are leaving ‘’ blank for the home directory for now, but later you will see that we will specify locations between the quotes.
Importantly, for the ‘views.home’ portion, ‘home’ refers to the function we created in ‘views.py’.
For example, if we were to create a second, simple “About Me” page we would do the following:
- Create ‘about.html’ in the templates folder like we did earlier
- Add a function to ‘views.py’
- Update ‘urlpatterns’ to reflect this
Returning to to our simple page at http://127.0.0.1:8000/ that was specified earlier when we ran our Django server we have….
I will be following up with Part 2 where we will be dealing with user input data. Thank you for reading my first Medium article! I hope it proved helpful.