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. INSTALL/SETUP PYTHON
The latest version of Python(Python 3) is installed on Ubuntu 18.04.1 LTS version itself. So it does not need to install it again.
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. You can choose the latest version as did(2.1 current latest).
pip3 install Django==2.1
4. INSTALL PYTHON-DJANGO-COMMON
You can install python-django-common using 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
Creating a Django project is easy using the below command. Here, awesome_project01 is the project name I chose for this project.
django-admin startproject awesome_project01
7. CHOOSING THE CODE EDITOR
You can choose your own favorite code editor for editing your project. But in my case, I choose Visual studio Code(VS code).
From the above link, you can download VS code .deb file compatible with Ubuntu/Debian.
8. DIRECTING TO YOUR PROJECT FOLDER AND EDITING
After installing VS code, direct to your project folder.
Open your project using VS code by just typing the below command.
From now on, you can use VS code Terminal instead of native Ubuntu Terminal because you can use the Terminal without going out of your code editor.
(Go to View -> Integrated Terminal from VS code’s top menubar)
9. MIGRATE DATABASE
You have to migrate the Database created by Django to your 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. EDIT ALLOWED_HOSTS
In Django, you need to set the ALLOWED_HOSTS in awesome_project01/settings.py to access the app using the hostname. I added 127.0.0.1 as shown below.
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 your browser and Enter the below URL and you will see the Django app’s home page.
Admin panel can be accessed using the below URL.
You can log in using the username and password created on step 10. After a successful login, it redirects to the Superuser dashboard which is shown below.
Have a nice code !