Web development became popular due to the widespread accessibility of the web, especially for commerce. Once businesses quickly realized they could offer their products and services on the web, this created a demand for web development that has never slowed down.
Web development can be divided into three major parts:
This guide will explore developing web apps using the Python framework Django. This framework mainly addresses backend and API/middleware development. It is therefore assumed that you have at least intermediate level knowledge in Python.
Web development in Python picked up to bring the data wrangling and processing power of Python to the web. Some popular sites built on Python/Django include Disqus, Instagram, and Mozilla, among others. More information can be found here.
To get started with Django, install it using this command on your terminal.
pip install django
To start a new Django project, run the starter command, which creates a boilerplate project.
django-admin startproject hello_world
You now have a Django project with basic boilerplate code. Within a Django project, there can exist several apps.
An app can be described as a library of code representing a discrete part of a larger project.
A Django app requires at least one app to run. To create an app, use the
manage.py script and pass the
startapp command, as demonstrated below.
python manage.py startapp myapp
At this point, you have a Django project called
hello_world and, within it, an app called
The app folder comes with starter files that are critical for any Django app. These are:
app.py: Where app configurations are defined
admin.py: Where model configurations are defined in relation to the project admin page
models.py: Where database tables are defined as
views.py- Where Django views are defined. These are objects that define how content is displayed on a web page.
For this project, you will add a new file and name it
urls.py. Copy the code below into the file under the
1 2 3 4 5
from django.urls import path from .views import home urlpatterns = [ path('', home,name='home'), ]
The function of this file is to define which view will be accessed by a user if they access a certain URL. In this case, when they access the root URL, they will be directed to the home view.
The next step is to develop the
views.py and copy the code block below.
1 2 3 4 5
from django.shortcuts import HttpResponse # Create your views here. def home(request): return HttpResponse("Hello world from Django")
The above creates a view called
home, which returns an
HTTPResponse. This means it will be just plain text.
In Django, there are class-based and function-based views. Read more about them here.
The app is now complete but needs to be connected to the main project since at runtime, the user accesses the project at root level and then through URL resolving and is directed to the appropriate app.
To connect the app to the main project, add it to the
INSTALLED_APPS list in the
settings.py file in the
hello_world/ project folder; also, link it in the main
urls.py file in the project.
To add to the
INSTALLED_APPS list, navigate to the
settings.py file and replace the current list with the list below.
1 2 3 4 5 6 7 8 9 10
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # local app 'myapp', ]
To link the project URL to the app URL, navigate to the
hello_world/urls.py file and copy the following code block.
1 2 3 4 5 6 7
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('myapp.urls')) ]
With the second item in the list, you are directing that if there is no parameter from the root URL (path empty), traffic should be directed to the URL's file in the
myapp URL's file, the root URL is resolved to display the
home view. This is how you will display the
Hello world from Django message.
To run the project, run the command:
python manage.py runserver
After running the
runserver command, upon accessing the
default Django URL, you should see a hello world page similar to the one below.
Web development in Django is quite a wide field. The skills learned in this guide offer an introduction to what is required for positions such as a backend or full-stack Python/Django web developer. To further build on this guide, research the following aspects of web development using Django: