Install Django Web Framework on Ubuntu

Django Is a free open source web framework that makes building web applications easier and faster with less coding. Is a fast and secure framework written using Python Programming language. In this short tutorial, we’ll see how to install the Django framework on an Ubuntu 18.04 LTS server. While this works on other Debian / Ubuntu based systems and derivatives like Linux Mint.

Install Django Web Framework on Ubuntu

You can install Django on Ubuntu in two ways.

  1. Use of Ubuntu official repository.
  2. Use pip (recommended method).

Both methods are displayed.

1. Install Django Web Framework on Ubuntu using official repositories

Django is available in the official Ubuntu repository. Therefore, it can be installed using the command.

$ sudo apt-get update
$ sudo apt-get install python-django

Sample output:

Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following additional packages will be installed:
 javascript-common libjs-jquery libpython-stdlib python
 python-django-common python-minimal python-sqlparse python-tz
 python2.7 python2.7-minimal
Suggested packages:
 apache2 | lighttpd | httpd python-doc python-tk python-psycopg2
 python-pymysql python-flup python-sqlite python-memcache python-pil
 python-bcrypt python-yaml geoip-database-extra
 | geoip-database-contrib gettext python-django-doc ipython bpython
 libgdal1 python-sqlparse-doc python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
 javascript-common libjs-jquery libpython-stdlib python python-django
 python-django-common python-minimal python-sqlparse python-tz
 python2.7 python2.7-minimal
0 upgraded, 11 newly installed, 0 to remove and 30 not upgraded.
Need to get 3,951 kB of archives.
After this operation, 29.7 MB of additional disk space will be used.
Do you want to continue? [Y/n]

When prompted to continue installing Django, type Y and press Enter.

Verify Django

I installed Django on Ubuntu. Run the following command from Terminal, whether or not Django was successfully installed.

$ django-admin --version

Sample output:

1.11.11

As you can see in the output above, Django 1.11.11 Installed.

One problem with installing Django from the Ubuntu official repositories is that the versions in the official repositories are slightly older. To install the latest stable Django version, install Django as described in the second method below.

Recommended downloads – Free eBook: “Python Programming Cookbook”

2. Install Django Web Framework on Ubuntu using pip

This is officially recommended by the Django project team. The latest stable version of Django can be obtained using the Python package manager Pip.

Update the repository list with the following command:

$ sudo apt-get update

Install pip using one of the following commands:

for Python 2 The following versions:

$ sudo apt-get install python-pip

for Python 3:

$ sudo apt-get install python3-pip

Next, if you use python 2, install Django.

$ sudo pip install django

If you use python 3, use the following command instead:

$ sudo pip3 install django

You can use either Python 2 or Python 3. Use Python 3 For the purpose of this tutorial.

To check your Django version, do the following:

$ django-admin --version

Sample output:

2.0.5

to see? This is a higher version than the Ubuntu official repository version.

Django is successfully installed. So what? Let’s go ahead and look at the basic usage of Django.

Basic usage of Django

Let’s create a new project called ostechnix.

To do so, do the following:

$ django-admin startproject ostechnix

The above command will create a directory called “ostechnix” in the current directory.

Let’s check the contents of this directory.

To do so, do the following:

$ ls ostechnix/

Sample output:

manage.py ostechnix

As you can see from the output above, there is a script called “manage.py” and another directory called “ostechnix”. The second “ostechnix” directory contains the actual code.

Next, change to the “ostechnix” directory.

$ cd ostechnix/

Next, run the following command to bootstrap the database:

$ python3 manage.py migrate

Note: If you use python 2 or earlier version, you need to run “Python manage.py migrate” Without quotes.

You will see output similar to the following:

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, you need to create an administrative user.

To do so, do the following:

$ python3 manage.py createsuperuser

Enter your username (leave blank to use your current username) and password.

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

Finally, start the Django development server with the following command:

$ python3 manage.py runserver 0.0.0.0:8000

Sample output:

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.

Congrats! The Django development server has been started. To stop the Django server, press Ctrl + C.

Access the Django development server page

Open a web browser, http: // IP-Address: 8000.

Oops! There is an error !!

Install Django Web Framework on Ubuntu

DisallowedHost at /

Invalid HTTP_HOST header: '192.168.225.26:8000'. You may need to add '192.168.225.26' to ALLOWED_HOSTS.

Your edit settings.py File. It will be available in the project directory (ie ostechnix in my case).

$ nano ostechnix/settings.py

Find the following line:

[...]

ALLOWED_HOSTS = []

[...]

Change as shown below.

[...]

ALLOWED_HOSTS = ['192.168.225.26']

[...]

Replace the above IP address with your own.

Save and close the file. Refresh the page and reload the URL.

You will now see the following screen.

Install Django Web Framework on Ubuntu

that’s it. Django is working!

To access the Django server administration page, http: // IP-Address: 8000 / admin.

Enter the username and password created earlier.

Install Django Web Framework on Ubuntu

Here is what my Django administration page looks like.

Install Django Web Framework on Ubuntu

did it! Django is ready to use. Start creating a web application.

See the official Django documentation for more information.

Now you know how to install Django from the Ubuntu official repositories and use the python package installation manager and how to create a basic project using Django.

Here’s another interesting article. Until then, look forward to OSTechNix.

cheers!

resource:

.

Sidebar