Install Django Web Framework In Ubuntu 16.04
Django is a free, open source web framework that allows you to build web applications more easily and quickly with less coding. It is fast, secure framework written using Python programming language.
In this brief tutorial, let us see how to install Django framework in Ubuntu 16.04 LTS server. Although this will work on other Debian/Ubuntu based systems and its derivatives like Linux Mint.
Install Django Web Framework In Ubuntu
We can install Django in Ubuntu in two methods.
- Using Ubuntu official repositories ;
- Using pip (Recommended method).
We will see both methods.
1. Install Django Web Framework In Ubuntu using official repositories
Django is available on Ubuntu official repositories. So, we can install it using command:
sudo apt-get update
sudo apt-get install python-django
Press Enter when it asks you to continue.
We have installed Django on Ubuntu. Run the following command from the Terminal whether Django has been successfully installed or not.
As you see in the above output, Django 1.8.7 has been installed.
The one problem with installing Django from Ubuntu official repositories is the version in the official will be lower than from the Django official latest version. In order to install most recent stable Django version, install Django as described in the second method below.
2. Install Django Web Framework In Ubuntu using pip
This is officially recommended by Django project team. You can get most recent stable version of Django by using Python package manager called pip.
Update the repositories list using command:
sudo apt-get update
Install pip using any one of the command below.
For Python 2 or lower versions:
sudo apt-get install python-pip
For python 3:
sudo apt-get install python3-pip
Now, install Django if you use python 2:
sudo pip install django
If you use python 3, then use following command instead:
sudo pip3 install django
You can use either Python 2 or Python 3. I use Python 3 for the purpose of this tutorial.
To verify the Django version, run:
See, this is higher version than the one in the Ubuntu official repositories.
We have successfully installed Django. Now, what? Let’s go ahead and see the basic usage of Django.
Django Basic usage
Let us create a new project called ostechnix.
To do so, run:
django-admin startproject ostechnix
The above command will create a directory called “ostechnix” in the current directory.
Let us check the contents of this directory.
To do so, run:
As you see in the above output, there is a script called “manage.py” and another directory called ‘ostechnix’. The second ‘ostechnix’ directory will have the actual code.
Now, change to the ‘ostechnix’ directory:
Then, run the following command to bootstrap the database:
python3 manage.py migrate
Note: If you use python 2 or earlier versions, you should use “python manage.py migrate” without quotes.
You will see an output like below.
Operations to perform: Apply all migrations: admin, auth, sessions, contenttypes Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying sessions.0001_initial... OK
Next, we need to create an administrative user.
To do so, run:
python3 manage.py createsuperuser
Enter the username (leave blank to use the current username), password
Username (leave blank to use 'sk'): Email address: email@example.com Password: Password (again): Superuser created successfully.
Finally, run the following command to start the Django development server.
python3 manage.py runserver 0.0.0.0:8000
Performing system checks... System check identified no issues (0 silenced). May 12, 2016 - 10:05:23 Django version 1.9.6, using settings 'ostechnix.settings' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C.
Congratulations! Django development server has been started. To stop the Django server, press CTRL+C.
Access Django development server page
Open up your web browser and navigate to http://IP-Address:8000.
You will be pleased with the following screen.
That’s it. Django is working.
To access thee Django server’s admin page, go to http://IP-Address:8000/admin.
Enter the username and password that you have created earlier.
This is how my Django admin page looks like.
Done! Django is ready to use. Start creating the web apps.
For more detailed, refer the Django official documentation.
You know now how to install Django from the Ubuntu official repositories and using python package installation manager and how to create a basic project using Django.
I will be here with another Interesting article soon. Until then, stay tuned with OSTechNix.