There are various python web frameworks and Django is one of the most popular ones. If we compare Django with Flask, we can say full-stack vs lightweight. If you want a full-stack web framework, then go with Django and if you want to go for lightweight applications and want to use microservices, then use Flask.
Django 3.0 was released in December 2019 with some additional amazing features.
We will discuss building a basic web application using Django 3.0 with Python 3.7 version on Ubuntu linux distribution.
Let’s start from scratch with complete basics from setting up your environments to installations to running the application.
Before we dig in, let’s see what we will discuss in this article in building a django web application.
Installations, packages and platforms:
- Django 3.0
- Python 3.7
- Ubuntu
- Pip
- Virtualenv
Install Python3
sudo apt install python3.7
To check Python version
python3 --version
Install pip3
sudo apt install python3-pip
To upgrade pip
python3 -m pip install --upgrade pip
To check pip version:
pip3 --version
Install virtualenv package to create virtual environments,
sudo pip install virtualenv
(This installs virtual environments package)
then run the below command to create a virtual environment for this application.
virtualenv django_env
(here “django_env” is the name of the virtual environment, so feel free to put your own desired name)
Type the below list command to confirm that the virtualenv is created
ls
(you should see the django_env folder there which confirms that the virtualenv is created)
Once you created the virtual environment, you can activate it by using,
source django_env/bin/activate
Install Django 3.0: (when you use pip3 to install django, it will take the latest versions)
sudo pip3 install django
This will install the latest Django version. Here this is Django 3.0.5 version
To check version:
python3 -m django --version
(3.0.5)
There are several commands which can be used to start off using django-admin. If you type the below command you will see the list of subcommands.
django-admin
We will need to start a new project so let’s go with startproject subcommand
django-admin startproject djangoproject
This creates a project folder with name “djangoproject”.
You should see a number of files in your current directory, such as django_env and djangoproject folders in it.
Now let’s go into the project folder and see what we have,
cd djangoproject
And lets see the tree of this project now,
.
├── djangoproject
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
Enter the below command to run the project by running manage.py from the djangoproject directory
python3 manage.py runserver
Now go to your browser and go to your localhost on port 8000 as the default port number is 8000 for django projects.
Or
You should see the default django web framework homepage which defines that your install is successful.
To access the admin page which comes inbuilt with Django project, add “/admin” to the home url as given below.
This admin url page is mentioned in urls.py file.
.
├── djangoproject
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.p
urls.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
Time to start an Application:
Now that the base project is ready and set up, let’s start an application in our djangoproject. Enter the below command from djangoproject directory where manage.py exists.
python3 manage.py startapp djangoapp1
Now if you observe, djangoapp1 directory is created with a number of python files in it.
.
├── db.sqlite3
├── djangoapp1
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── djangoproject
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── settings.cpython-36.pyc
│ │ ├── urls.cpython-36.pyc
│ │ └── wsgi.cpython-36.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
Let’s create a basic page in djangoapp1 and run the server.
We need to setup 3 files to create a page which are
– views.py ,
– urls.py (in djangoproject directory) and
– urls.py (in djangoapp1 directory)
In views.py we will define functions for each url HTTP Response request.
In views.py,
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('<h1>Welcome to my Djangoapp</h1>')
Now create a “urls.py” file under the djangoapp1 folder
.
├── db.sqlite3
├── djangoapp1
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── djangoproject
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── settings.cpython-36.pyc
│ │ ├── urls.cpython-36.pyc
│ │ └── wsgi.cpython-36.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
In urls.py (djangoapp1), we will import the path from django.urls and all views which we defined in views.py file in djangoapp1 app directory. You can add many url patterns in this file, but for now we will add only one page which displays your welcome page. Your urls.py in djangoapp1 should look like this.
In urls.py (djangoapp1),
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='homepage'),
]
In urls.py file (djangoproject),
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(' ',include('djangoapp1.urls')),
]
Now from the terminal, run the below command.
python3 manage.py runserver
Great! Here we just tried to display a heading content in our page by mentioning in the views.py file.
Now let’s redirect this to a HTML page instead of hard coding the “Welcome to my Djangoapp” content in views.py
Create a folder called “templates” in the project app directory “djangoapp1” and then create a subfolder inside the templates folder with the project app name “djangoapp1”. Now create a file “index.html” inside this djangoapp1 folder.
The tree should look like this:
.
├── db.sqlite3
├── djangoapp1
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── admin.cpython-36.pyc
│ │ ├── models.cpython-36.pyc
│ │ ├── urls.cpython-36.pyc
│ │ └── views.cpython-36.pyc
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ └── __init__.cpython-36.pyc
│ ├── models.py
│ ├── templates
│ │ └── djangoapp1
│ │ └── index.html
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── djangoproject
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── settings.cpython-36.pyc
│ │ ├── urls.cpython-36.pyc
│ │ └── wsgi.cpython-36.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
In the index.html file, you can add HTML content and styling to it. For now let’s keep it simple with h1 tag and p tag.
index.html
<h1>Welcome to my Djangoapp Homepage</h1>
<p>This is the homepage HTML template</p>
Now you have a HTML page ready. You can add a number of HTML pages and CSS to them as well depending on your own choice and each page needs to be mentioned in views.py and urls.py as we did for index.html.
In the last step, we need to mention our app in settings.py.
In settings.py,under the INTSALLED_APPS section, add your app name “djangoapp1”.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'djangoapp1',
]
It’s time to run manage.py again but this time you should see the index.html page content on your browser.
python3 manage.py runserver
Head to the http://localhost:8000/ in your browser to see your application.
Welcome to my Djangoapp Homepage
This is the homepage HTML template
There you go! You should see the index.html heading and paragraph content in the page. You can use your skills to start your HTML content and CSS styling in this application now.
Here’s the djangoproject repository from Github
https://github.com/amarnath091/djangoproject.git
There are many other functionalities in Django, but as mentioned in the beginning, this article is a starting point for beginners to build a basic Django application and this initiation can lead in implementing innovative applications.
In my future posts, I will discuss more about how to dockerize a Django application and the next steps in DevOps practices. Please follow DevOpsPal social media to stay updated with the latest updates.