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).
1. Update and Upgrade
First, we need to update the list of available packages and their versions. Then install the newer version of packages. These can be done using the commands below.
sudo apt-get update sudo apt-get upgrade
2. Install Python
The latest version of Python(Python 3.x) will be installed on 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.
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 with the below commands.
sudo apt-get install python3.6
3. Install Pip
pip is the package management system used to install and manage software packages written in Python. Here we need pip to install Django on our Ubuntu system.
sudo apt-get install python3-pip
4. Install Django
Now Django can be simply installed using pip. Currently, 2.1 is the latest version of Django and it can be installed using the below command.
pip3 install Django==2.1
5. Install Python-django-common
Then we need to install python-django-common, which is a package containing things which speed up the development with Django.
sudo apt install python-django-common
6. Install Python-django
python-django can be installed using the below command.
sudo apt install python-django
7. Creating 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
8. Migrate Database
We have to migrate the database created by Django to our local server. This can be done using the below command.
python manage.py migrate
9. Setting Up a Superuser Account
Django as default provides a Superuser account. This superuser can manage user roles, admins, etc. It can be set up by the below command.
python manage.py createsuperuser
Enter the username, email, and password.
Username (leave blank to use 'syam'): syamlal Email address: [email protected] Password: Password (again): Superuser created successfully.
10. 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.
We need to add 127.0.0.1 (localhost IP) in ALLOWED_HOSTS array in the settings.py file inside our project.
ALLOWED_HOSTS = ['127.0.0.1']
Note: We have to add our domain name instead of this localhost IP before deploying this project to our server. I am not explaining about it right now.
11. Running the server
Now its time to run the server on our local machine with the command below.
python manage.py runserver 127.0.0.1:8000
Then open up a browser window and enter the below URL. We can see the Django app’s home page.
Admin panel can be accessed using the below URL.
We can log in using the username and password we created in step 9. After a successful login, it redirects to the Superuser dashboard which is shown below.
Have a nice code !