How to Install and Setup Django on Ubuntu 18.04.1 LTS (Bionic Beaver)

Image is loading...

Django is a free and open-source web framework, written in Python, which follows the model-view-template (MVT) architectural pattern. Django’s primary goal is to ease the creation of complex, database-driven websites. Django emphasizes reusability and “pluggability” of components, less code, low coupling, and rapid development. Python is used throughout, even for settings files and data models. Django also provides an optional administrative CRUD interface that is generated dynamically through introspection and configured via admin models. Here I am going to explain the steps in installing Django on the latest version of Ubuntu 18.04.1 LTS (Bionic Beaver).



INSTALLATION

1. INSTALL/SETUP PYTHON

The latest version of Python(Python 3.x) is installed on the Ubuntu 18.04.1 LTS version itself. So it does not need to install it again. We can check whether Python3 is installed on our system or not using the below command.

python3 --version

If it is not, we can install it using the below commands. The current latest version of python is python 3.6 and it can be installed using the below commands.

sudo apt-get update
sudo apt-get install python3.6

2. INSTALL PIP

pip is a package management system used to install and manage software packages written in Python. Here we are using pip to install Django on our Ubuntu system.

sudo apt-get install python3-pip

3. INSTALL DJANGO

Django can be simply installed using pip using the below command. Currently, 2.1 is the latest version of Django and it can be installed using the below command.

pip3 install Django==2.1

4. INSTALL PYTHON-DJANGO-COMMON

Now we need to install python-django-common, which is a  package contains things which speed up the development with Django.install it with the below command.

sudo apt install python-django-common

5. INSTALL PYTHON-DJANGO

python-django can be installed using the below command.

sudo apt install python-django

6. CREATE FIRST DJANGO PROJECT

Now, creating a Django project is easy using the below command. Here, awesome_project01 is the project name I chose for my project.

django-admin startproject awesome_project01

7. CHOOSING THE CODE EDITOR (OPTIONAL)

You can choose your own favorite code editor for editing your project. But in my case, I choose the Visual studio Code (VS code).

https://code.visualstudio.com/#alt-downloads

From the above link, you can download VS code .deb file compatible with Ubuntu/Debian.

8. DIRECTING TO YOUR PROJECT FOLDER AND EDITING (OPTIONAL)

After installing VS code, direct to your project folder.

cd awesome_project01

Open your project using VS code by just typing the below command.

code .

From now on, we can simply use VS code Terminal instead of native Ubuntu Terminal.
(Go to View -> Integrated Terminal from VS code’s top menubar)



9. MIGRATE DATABASE

We have to migrate the database created by Django to our local server using the below command.

python manage.py migrate

10. SETUP SUPERUSER ACCOUNT

Django also provides a Superuser account as default in it. You can manage the user roles, admins etc. using this account afterward. Superuser account can be set up by the below command.

python manage.py createsuperuser

This will ask for Username, Email, and Password for the account. Just Enter it and Done.

Username (leave blank to use 'syam'): syamlal
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.

11. SETUP ALLOWED_HOSTS

In Django, you need to set the ALLOWED_HOSTS, a list of strings representing the host/domain names that this Django site can serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations.

This can be done from awesome_project01/settings.py. Add 127.0.0.1 (localhost IP) as shown below. We have to add the domain name instead of this localhost IP before deploying this project to our server. I am not explaining about it right now.

ALLOWED_HOSTS = ['127.0.0.1']

12. RUN THE SERVER

Now its time to run the server on our local machine by running the below command.

python manage.py runserver 127.0.0.1:8000

Open the browser and Enter the below URL and you will see the Django app’s home page.

http://127.0.0.1:8000/
Image is loading...
Your Django Home Page

Admin panel can be accessed using the below URL.

http://127.0.0.1:8000/admin
Image is loading...
Django Superuser Login Window

We can log in using the username and password we created on step 10. After a successful login, it redirects to the Superuser dashboard which is shown below.

Image is loading...
Django Superuser Dashboard

 

Have a nice code !

You May Also Like

About the Author: Syamlal CM

Hi, I'm Syamlal, the co-founder of Redmonark. I'm a developer, writer and also have a little bit of experience in graphic designing.

1 Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.