How to Deploy Your First App in Django

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It’s built on the principle of DRY (Don’t Repeat Yourself), emphasizing reusability and pluggability of components. Django follows the Model-View-Template (MVT) architectural pattern, similar to Model-View-Controller (MVC), but with its unique approach.

Linux is a preferred choice for hosting Django applications due to its stability, security, and flexibility. Additionally, many web servers, databases, and deployment tools are optimized for Linux environments. Deploying Django on Linux offers a robust foundation for scaling and managing web applications efficiently.

This guide provides you with detailed instructions on how to deploy a Django app on Ubuntu 22.04.

Deploying First App in Django

Following are instructions on Django deploy application given below:

Setting Up Python

Before deploy Django application ensure that you have install Python in Ubuntu operating system, after that you also need to manage libraries so install PIP for that purpose. Python serves as the primary language for Django development, while PIP is the package installer used to manage dependencies.

You can use the below command to check if Python is already installed or not:

python --version

This command will show you the Python version if it is already installed. Otherwise, it instructs you to install it using:

sudo apt install python3

After that install the pip package using the following command:

sudo apt install python3-pip

This is necessary to install Django that you can do using the following command:

sudo pip install django
install django


To verify the Django version, you can use:

django-admin --version
django admin


Installing Virtualenv for Isolated Environments

Virtualenv is a tool to create isolated Python environments for each project, preventing conflicts between dependencies. It allows you to install project-specific packages without affecting the system-wide Python installation:

pip install virtualenv
install virtualenv


Now you need to create a virtual environment named “venv”. A virtual environment is an isolated environment where you can install and manage packages separately from your system-wide Python installation:

virtualenv venv
virtualenv venv


This command creates a fresh virtual environment called “venv” within the directory you’re currently working in. After the setup is finished, you’ll have to activate this virtual environment as well.

source venv/bin/activate
activate venv


Configuring Django Project

After installing Django you can now configure your Django project using the below command: 

django-admin startproject django_project
start django


Executing this command will create a standard folder arrangement, containing Python files and your project’s management application named identically to your project.

tree django_project/django_project/
tree django


Take some time to examine the initial project structure that the django-admin command-line tool has generated for you. Each project created with the startproject command will follow this same layout. Once you’re prepared, you can proceed to develop a Django app, which serves as a fundamental component of your new web application.

Initiating Django Application

In Django, each project can encompass multiple Django apps. When you used the startproject command earlier, you established a management app necessary for every default project. Now, you’ll craft a Django app to encapsulate the specific functionality of your web application. You no longer require the django-admin command-line utility; instead, you can initiate the startapp command via the manage.py file:

python manage.py startapp <appname>

The startapp command constructs a standard folder arrangement for a Django application. In this tutorial, the app is named “example.”

python manage.py startapp example
python start app


After executing this command, Django will create a new app with the name “example” within your project. This can be verified using the list command:

ls
project example


Don’t forget to substitute “example” with the name of your app when you’re creating the Django app for your personal web application. After the startapp command completes, you will notice that Django has included an additional folder within your existing folder structure.

The newly created folder adopts the name you provided during the command execution, which in this tutorial’s case is “example/”. Inside this folder, you’ll find a few Python files. This Django app directory will be your primary workspace for developing your web application. While you’ll also make adjustments in the management app, “setup/”, most of your work will be centered around the Django app, “example/”.

As you progress through a tutorial or start developing your own project, you’ll become more familiar with the Python files generated. Here are three important files created within the app folder:

  1. init.py: This file signals Python that the folder is a package, enabling Django to utilize code from various apps to construct the overall functionality of your web application. You likely won’t need to modify this file.
  2. models.py: In this file, you define the models for your app, enabling Django to interact with the database of your web application.
  3. views.py: Most of your app’s code logic will be written in this file.

With the framework of your Django web application set up, you’re ready to start realizing your ideas. The direction you take from here is entirely up to you as you work towards creating your own distinct project.

Creating a Superuser

After setting up your Django project and initiating the Django application, it’s essential to create a superuser in Django. The superuser account grants you administrative access to the Django admin interface, allowing you to manage your application’s data and configurations. To create a superuser, run the following command:

python manage.py createsuperuser
django super user


Follow the prompts to enter a username, email address, and password for the superuser account. Once the superuser account is created, you can use it to access the Django admin interface during development.

Running the Server

Once your Django project is configured and your superuser is created, you need to run the development server to preview your application locally. You can start the server using the following command:

python manage.py runserver
python run server


Executing this command will start the development server, and you’ll see an output indicating that the server is running. By default, the server will run on http://127.0.0.1:8000/, and you can access your Django application by navigating to this address in your web browser.

Conclusion

In this guide, we have discussed all the necessary steps that are needed to deploy first Django app on Ubuntu 22.04. These steps include setting up Python, configuring Django projects, and initiating your first application on Django.

Django emerges as a powerful tool for web development, promoting efficient coding practices through its DRY principle and MVT architecture. On the other hand, Linux serves as an ideal hosting environment for Django applications, offering stability and scalability.

Successfully deploying your first Django app is an exciting moment. For a smooth deployment experience consider Ultahost’s Python hosting service offers a perfect solution. Their plans are pre-configured for Python applications like Django, eliminating the need for manual setup.

FAQ

What are the steps involved in deploying a Django app?
How do I prepare my Django app for production?
Which web servers are commonly used to deploy Django apps?
What database servers are compatible with Django?

Related Post

How to Install Cryptography in Python

Cryptography keeps information safe when sent between c...

How to Create a Superuser in Django

Django is a high-level web framework for building web a...

How to Install Python on Debian

Python is a high-level programming language known for i...

How to Substring a String in Python

In Python, substrings are extracted portions of a strin...

How to Install Flask in Python

Flask is a popular Python module used for web developme...

How to Install Pandas in Python

Pandas is a popular Python library used for data manipu...

Leave a Comment