Django is one of the best and very popular web application framework of Python. in this article we will share with you how to install Django in your ubuntu system and how to run your first python web application in a web browser. Django provides us hight security in web application and it also provides many ready-made helper it's can help us to build web applications very fast.
This article will help you to install Django in ubuntu OS and how to run your first python web application in your web browser.
Before installing Django you should install python, the following link helps you in install python.
Step - 1 Install python and PIP
Ubuntu 16.04 have installed python 2 by default. but we need to python 3. so, first wee need to install python 3 in our ubuntu system. run the following command in your terminal to install pip.
sudo apt-get install python3 python3-pip
The installed Python version is:
python3 -V
Python 3.5.3
The installed PIP version is:
pip3 -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.5)
Step - 2 Install Django
Django is open-source and is also available on GitHub you can download from there using pip
and install Django
in your ubuntu system. in this article I use pip3
for the installation of Django in ubuntu. run the following command in the terminal.
pip3 install Django
Check version:
django-admin --version
2.2.6
Step - 3 Create a Django Application
Now, we will show how to create our first Django
application in our ubuntu system. the django-admin
command provides us to create one new fresh django
application. you can create an application anywhere in your system directory. just go to your right path and run the following command in terminal.
cd /var/www
django-admin startproject first_django_app
After, create your first Django
the application then we should run the default migration help of the following command in your terminal.
cd first_django_app
python3 manage.py migrate
Step - 4 Create a Super Admin
Now, we need to create one SuperUser / Admin
for the administration of the Django web application. run the following command on your project root directory for creating SuperUser / Admin
.
python3 manage.py createsuperuser
Step - 5 Run Django Application
Now, your Django application ready to run and use. by default Django not allow any external hosts to access or run Django application in a web browser. so first we add some hosts in first_django_app/settings.py
a file. just change the following code line and add hosts that use to run the Django application. I mostly use localhost
to run any web application. so, I will add my localhost
host in settings.py
a file.
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['192.168.1.104' , 'localhost']
look above changes I added my two hosts there. so I will able to run my Django web application on both hosts 192.168.1.104
and localhost
. now run the following command for run the Django web application in our web browser.
python3 manage.py runserver 192.168.1.104:8000
//Or
python3 manage.py runserver localhost:8000
After, hit above command in your terminal then now open http://localhost:8000
URL in your browser to run the Django application. after a run, it looks like the following screenshot.
Step - 6 Open The Admin
Django's By default admin route is /admin
.
http://localhost:8000/admin
Then enter your username and password there. which we created in Step - 4
. after login as a SuperUser
the screen looks like.
We hope you like this article.