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.
1pip install django
To start a new Django project, run the starter command, which creates a boilerplate project.
1django-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.
1python 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
1from django.urls import path 2from .views import home 3urlpatterns = [ 4 path('', home,name='home'), 5]
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.
1from django.shortcuts import HttpResponse 2 3# Create your views here. 4def home(request): 5 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.
1INSTALLED_APPS = [ 2 'django.contrib.admin', 3 'django.contrib.auth', 4 'django.contrib.contenttypes', 5 'django.contrib.sessions', 6 'django.contrib.messages', 7 'django.contrib.staticfiles', 8 # local app 9 'myapp', 10]
To link the project URL to the app URL, navigate to the
hello_world/urls.py file and copy the following code block.
1from django.contrib import admin 2from django.urls import path, include 3 4urlpatterns = [ 5 path('admin/', admin.site.urls), 6 path('', include('myapp.urls')) 7]
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:
1python manage.py runserver
This will execute the project using a Web Server Gateway Interface (WSGI) that is inbuilt and necessary to run Python projects on the web. The site is accessible by default on http://127.0.0.1:8000.
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: