Knowledge Base
MilesWeb / How-Tos

Know How to Deploy Python Django Application?

Approx. read time : 4 min

Django is a very powerful web framework that lets you to deploy the Python websites or application. Django makes the process of web development easy, and lets you focus on writing the code.

In this guide, you’ll learn how to deploy Python Django web application.

So, lets begin!

Creating a New Project

First, to create a new project, we will use the startproject command to create a new project called, djangoproject. Make sure to include the period (.) at the end of the command so that it’s installed in the current directory.

Initially, the .venv directory may not be visible, because its hidden, however its still there.

The .venv directory was first created with the virtual environment but Django has added a django_project directory and a manage.py file. Inside the django_project there are five new files.

__init__.py : Shows that the files in folder are a part of the Python package. If you don’t have these file, you cannot import the files from another directory.

asgi.py: ASGI stands for Asynchronous Server Gateway Interface . It is a specification for building asynchronous web services with Python.

settings.py : Controls the overall settings of Django project

urls.py: It tells Django which web pages to build in response to URL or browser request

wsgi.py: Stands for Web Server Gateway Interface. It’s basically helps Django to serve the web pages.

The manage.py file is not a part of django_project, but is used for executing different commands like running the local web server or creating a new application.

Let us now create a new project by using Django’s lightweight web server. We will use the command runserver that’s located in manage.py.

Then, you will get to see the successful installation of Python Django application.

Make a note that the command line output has the additional information that includes the warning about 18 unapplied migrations.

These warnings are very irritating, but we can remove the warning by initially stopping the local server with the command Control+c and then running the command python manage.py migrate.

Here Django has created a SQLite database and migrated its built-in apps that are provided to us. It is shown by the new file db.sqlite3 directly.

Now, if you want to execute python.manage.py runserver, again you will not get to see any type of warnings.

Creating an Application

Django basically uses the concept of apps and projects to keep the entire code clean and readable code. A single top-level Django web project has different applications. To take an example of an eCommerce website, it has one app for user authentication, other app for payments, and so on.

All of these applications live within one-top level project.

The helloworld directory Django has created within it a new pages directory that includes the below files:

Here we’ll get to know what every new pages app files does.

admin.py: Configuration file for the built-in Django Admin app

apps.py: A configuration file for the application

migrations: To keep track of changes to the model.py file so that it stays in sync with the database

tests.py: Is for performing the specific tests

views.py: That lets you handle the request/response logic for the web application

Make sure, even if the new application exists within the Django web project, Django doesn’t know it unless we add it to the django_project/settings.py file.

In your text editor, first open the file up and scroll down to INSTALLD_APPS where you’ll get to see the built-in Django applications available there.

Then, you need to add the pages.apps.PagesConfig right at the bottom.

If firstly, you have Black installed in your text editor on “save”, it will change all the single quotes (‘’) to double quotes (“”).

PageConfig here is the name of the solitary function within the pages/apps.py file.

Hello World

In Django, there are four separate files which align with the MVT pattern required to power single dynamic webpage as follows:

models.py

views.py

template.html

urls.py

Now, in the next step, you first have to create the first view. Start with updating the views.py files in the page application as given below:

Whenever the homePageview function is called, then return to the text “Hello World”!

Here, we have created a function called homePageView that accepts the request object and in return gets a response with the string “Hello World”

Configuring URLs

The URLs needs to be configured. Thus, in your text editor, first create a new file, called as url.py within the pages app. Then, you need to update it with the below code:

Now the last step is to update the django_project/urls.py  file. Its quite common to have several apps within a single Django project like, pages here, and each of them need their dedicated URL path.

First, we have imported the include on the second line next to path and then created a new URL for our pages app. Whenever, any user visits the homepage, they will be first routed to the pages application and then to the

homePageView, and set in thepages/urls.py file.

At last, to make sure if everything works fine, restart the Django server:

Then, after you refresh the server, you will get to see the “Hello World”.

So, this is how you can create Python Django application.

Nehal Khatri
Nehal is an ardent content writer. She's passionate about crafting content that's simple but adds value. Her insatiable interest in writing has allowed her to explore her skills. She is adept and can write for different types of content formats.