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 │ ├── __init__.py | ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py └── .venv/
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.
# Windows (.venv) > python manage.py runserver # macOS (.venv) % python3 manage.py 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 manage.py migrate.
# Windows > python manage.py migrate # macOS % python3 manage.py 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 │ ├── __init__.py | ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── db.sqlite3 # new ├── manage.py └── .venv/
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.
# Windows (.venv) > python manage.py startapp pages # macOS (.venv) % python3 manage.py startapp pages
The helloworld directory Django has created within it a new pages directory that includes the below files:
├── pages │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── tests.py │ └── views.py
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.
# django_project/settings.py INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", "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/apps.py file.
# pages/apps.py 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:
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:
# pages/views.py 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 url.py within the pages app. Then, you need to update it with the below code:
# pages/urls.py from django.urls import path from .views import homePageView urlpatterns = [ path("", homePageView, name="home"), ]
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.
# django_project/urls.py from django.contrib import admin from django.urls import path, include # new urlpatterns = [ path("admin/", admin.site.urls), 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/urls.py file.
At last, to make sure if everything works fine, restart the Django server:
# Windows
(.venv) > python manage.py runserver
# macOS
(.venv) % python3 manage.py 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.