Advance

How to install Django on Ubuntu 20.04

How to install Django on Ubuntu 20.04

As a live website, you need to have additional considerations, including connecting to a database. In this article, you will learn how to install Django on Ubuntu 20.04, and also how to set up a development environment.

 

Introduction

Django is a high-level Python Web framework that is free, open-source, and rapid development. It is also known for its loose coupling and framework-level consistency for individual components. Django is necessary for setting up a domain name and adding layers of security. Ridiculously fast, reassuringly secure, and exceedingly scalable are the most useful features of Django.

 

Prerequisites

The tutorial may be more useful if you know:

1- Be careful to execute commands with root access in the user interface, you must use the Sudo command before all commands.

2- Python 3 set up with a virtual programming environment

 

Also, you can see more information at Initial server setup on Ubuntu 20.04

How to install Django on Ubuntu 20.04

Let’s walk through the steps of this guide to learn how to install Django on Ubuntu 20.04

Recommended Article: How to install Ansible on Ubuntu 20.04

Step 1-  Installing Django

Between the several ways to install Django, the Python package manager pip within a virtual environment, we would go on in a safe and smooth way. you need to create the directory which will contain your Django application through the server’s home directory. So to create a directory run the following command. You can choose the name of your choice (instead of Django-apps)to navigate to the directory.

mkdir django-apps
cd django-apps

Create your virtual environment inside the django-apps directory. Again you need to choose a suitable name. (instead of env).

virtualenv env  

Active the virtual environment bt entering the following command:

. env/bin/activate  

While you change the prefix to (env), it will be active, and depending on what directory you are in, the result would be as below:

(env) [email protected]:$

Install the Django package using pip, in the environment. It helps you to create and run Django applications.

(env) [email protected]:$ pip install django

After installing, verify your Django installation by running a version check:

(env) [email protected]:$ django-admin --version

Something like below will be the output:

Output

3.0.6

 

Step 2–  Adjusting Firewall Settings

You can open the port with the following command for the UFW firewall:

(env) [email protected]:$ sudo ufw allow 8000

Step 3-  Starting the Project

In this part, you can generate an application called django-admin. Run the following command to create the project directory structure.

(env) [email protected]:$ jango-admin startproject testsite

To obvious what files were created, Navigate to the testsite directory.

(env) [email protected]:$ cd testsite  (env) [email protected]:$ ls
Output
manage.py  testsite

Then, you would view manage.py script by running as below.

(env) [email protected]:$ less manage.py

Press q to quit viewing the file after finishing reading the script.

To view the other files, navigate to the testsite directory.

(env) [email protected]:$ cd testsite/

To list the contents of the directory, enter the following command.

(env) [email protected]:$ ls

Then, you will see 4 files.

Output

__init__.py asgi.py settings.py urls.py wsgi.py    
Recommended Article: How to install Django on Ubuntu 20.04

Let’s know what are these 4 files.

__init__.py is the entry point for your Python project.

asgi.py is a good successor of WSGI. Which is about the configuration of the optional deployment to the Asynchronous Server Gateway Interface.

settings.py lets Django know which settings are available and describes the configuration of your Django installation.

urls.py contains a urlpatterns list, that routes and maps URLs to their views.

wsgi.py contains the configuration for the Web Server Gateway Interface, which provides a standard for synchronous Python apps.

 

Buy VPS Hosting

 

Step 4Configuring Django

Now you can view the website on a designated host after starting the server by entering the runserver command.

add your server IP address to the list of ALLOWED_HOST in the settings.py file located in

 

We’ll need to add your server IP address to the list of ALLOWED_HOSTS in the settings.py file located ~/test_django_app/testsite/testsite/

Also, you can use your considered text editor to add the IP address. If you are using nano, run the command below.

(env) [email protected]:$ nano ~/django-apps/testsite/testsite/settings.py

If you want to change and exit nano, hold down the CTRL+X keys and then press the y key.

Finally, you can create an administrative user by using createsuperuser command.

(my_env) $ python manage.py createsuperuser

you will be asked to enter a username, an email address, and a password for your user.

 

Step 5Accessing the Django Web App

Be sure to navigate back to the directory where manage.py is located, when the configuration is completed.

(env) [email protected]:$ cd ~/django-apps/testsite    

To replace your server-ip text with the IP of your server, run the following command.

(env) [email protected]:$ python manage.py runserver 0.0.0.0:8000    

Then, navigate to the following link to view what your skeleton website looks like, and replace the highlighted text with your server’s actual IP:

http://your-server-ip:8000/

 

You would see the page below as a result.

 

It shows that Django has been properly installed and your test project is working correctly. Also, you can add /admin/ to the end of your URL to access the admin interface.

http://your_server_ip:8000/admin/

 

 

You will have access to the main admin section of the site if you enter the admin username and password that you just created.

 

To return to your programming environment and testing your app, press CTL + C to stop the runserver command.

To leave your Python environment, run the deactivate command.

(env) [email protected]:$ deactivate

If you deactivate the programming environment, it will put you back to the terminal command prompt.

 

Dear user, we wish this tutorial how to install Django on Ubuntu 20.04 would be helpful for you, to ask any question or review the conversation of our users about this article, please visit Ask page. Also to improve your knowledge, there are so many useful tutorials ready for Eldernode training.

Do not miss

We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave Your Comment

Your email address will not be published.

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+18054214518

7 days a week, 24 hours a day