Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  How-Tos  >  Know How to Deploy Python Django Application?
Top Scroll

Know How to Deploy Python Django Application?

 6 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.

(.venv) > django-admin startproject django_project .

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

├── django_project
│   ├──
|   ├──
│   ├──
│   ├──
│   └──
└── .venv/

The .venv directory was first created with the virtual environment but Django has added a django_project directory and a file. Inside the django_project there are five new files. : 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 stands for Asynchronous Server Gateway Interface . It is a specification for building asynchronous web services with Python. : Controls the overall settings of Django project It tells Django which web pages to build in response to URL or browser request

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

The 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

# Windows
(.venv) > python runserver

# macOS
(.venv) % python3 runserver

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 migrate.

# Windows
> python migrate

# macOS
% python3 migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

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.

├── django_project
│   ├──
|   ├──
│   ├──
│   ├──
│   └──
├── db.sqlite3  # new
└── .venv/

Now, if you want to execute 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.

# Windows
(.venv) > python startapp pages

# macOS
(.venv) % python3 startapp pages

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

├── pages
│   ├──
│   ├──
│   ├──
│   ├── migrations
│   │   └──
│   ├──
│   ├──
│   └──

Here we’ll get to know what every new pages app files does. Configuration file for the built-in Django Admin app A configuration file for the application

migrations: To keep track of changes to the file so that it stays in sync with the database Is for performing the specific tests 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/ 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.

# django_project/
    "pages.apps.PagesConfig",  # new

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/ file.

# pages/
from django.apps import AppConfig

class PagesConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "pages"

Hello World

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


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

# pages/
from django.http import HttpResponse

def homePageView(request):
    return HttpResponse("Hello, World!")

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 within the pages app. Then, you need to update it with the below code:

# pages/
from django.urls import path
from .views import homePageView

urlpatterns = [
    path("", homePageView, name="home"),

Now the last step is to update the django_project/  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.

# django_project/
from django.contrib import admin
from django.urls import path, include  # new

urlpatterns = [
    path("", include("pages.urls")),  # new

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/ file.

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

# Windows
(.venv) > python runserver

# macOS
(.venv) % python3 runserver

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

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

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
Claim the discount before it’s too late. Use the coupon code:
Note: Copy the coupon code and apply it on checkout.