How to install Django on ubuntu

Add Your Heading Text Here

Django is a high-level, open-source web framework for building robust and scalable web applications with Python. Known for its simplicity and “don’t repeat yourself” (DRY) philosophy, Django streamlines the development process by providing a clean and organized structure. It follows the model-view-controller (MVC) architectural pattern, emphasizing code reusability and readability. Django includes an object-relational mapping (ORM) system for database interactions, authentication mechanisms, and an automatic admin interface, reducing the need for repetitive coding tasks.

The framework encourages rapid development through its batteries-included approach, offering built-in features such as URL routing, form handling, and template engines. Django’s strong emphasis on security includes protection against common web vulnerabilities. With an active and supportive community, extensive documentation, and a wealth of third-party packages, Django remains a popular choice for developers seeking efficiency and maintainability in web application development.

update the local package

				
					sudo apt update && apt upgrade -y

#Check the version of Python you have installed:
python3 -V

#install pip and venv:
sudo apt install python3-pip python3-venv -y
				
			

cloning the Django repository

				
					git clone  https://github.com/django/dja ~/django-dev

#Change to this directory:
cd ~/django-dev

#Create a virtual environment:
python3 -m venv vr_env

#Activate it:
source vr_env/bin/activate

#install the repository:
pip install -e ~/django-dev

#verify that the installation:
django-admin --version
				
			

Creating a Sample Project

				
					mkdir ~/django-test
cd ~/django-test

#create your virtual environment:
python3 -m venv my_env

#Activate the environment:
source my_env/bin/activate

#Install Django:
pip install django
				
			

To build your project, you can use django-admin with the startproject command. We will call our project djangoproject, but you can replace this with a different name. startproject will create a directory within your current working directory that includes:

A management script, manage.py, which you can use to administer various Django-specific tasks.
A directory (with the same name as the project) that includes the actual project code.
To avoid having too many nested directories, however, let’s tell Django to place the management script and inner directory in the current directory (notice the ending dot):

				
					django-admin startproject djangoproject .

#To migrate the database, type:
	python manage.py migrate

#create an administrative user:
	python manage.py createsuperuser
				
			

Modifying ALLOWED_HOSTS in the Django Settings
To successfully test your application, you will need to modify one of the directives in the Django settings.

				
					#Open the settings file by typing:
nano ~/django-test/djangoproject/settings.py

~/django-test/djangoproject/settings.py
ALLOWED_HOSTS = ['your_server_ip_or_domain', 'your_second_ip_or_domain', . . .]

#save and exit.

#Testing the Development Server
sudo ufw allow 8000

#Start the development server:
python manage.py runserver your_server_ip:8000
				
			

Visit:

http://your_server_ip:8000

http://your_server_ip:8000/admin/

Leave a Comment

Your email address will not be published. Required fields are marked *