Introduction
Django is a free and open-source web framework written in the python programming language. It follows the model-template-views architectural pattern and allows rapid development. Django is versatile in nature that can be used to create applications in multiple domains. It takes less time to develop a web application than other web frameworks.
This article will explain how to install and use Django on ubuntu.
Prerequisites
- A root or non-root user with
sudo
privileges. - We need to set up Python and Pip on the Ubuntu 20.04 system.
Install Python
- Update the software repository.
$ sudo apt update
- It is recommended by the Django Software Foundation to use Python 3, we can install Python 3 by using the following command.
$ sudo apt-get install python3
- Check which version of Python you have installed.
$ python3 -V
Output:
Install PIP
- Need pip in order to install packages from PyPi, Python’s package repository, to install pip follow the below command.
$ sudo apt-get install -y python3-pip
Install Django
- To install Python Django on ubuntu by using the below command.
$ sudo apt install python3-django -y
- Check which version of Django you have installed.
Output:$ django-admin --version
- Run the following command to create the new project. This command creates a new directory called "project_name".
$ django-admin startproject project_name
- Run the below command to create an application for the project.
$ python3 project_name/manage.py startapp app_name
- Migrate the project database.
$
python3 project_name/manage.py migrate
- Open
setting.py
file using the below command.
locate the$ sudo vi project_name/project_name/settings.py
ALLOWED_HOSTS
inside the file and list your serve IP addresses or domain names in the ALLOW_HOSTS then save and close the file. - Start the Django development server.
$ sudo python3 project_name/manage.py runserver your_server_ip:8000
Replace your IP address with <your_server_ip> in the above command."
Output: - Open the web browser and visit your server IP address followed by :8000 port.
Output:http://your_server_ip:8000