How to Install Django Web Application Framework on Debian 10

howtoinstalldjangoondebian10

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It's free and open source.

In this tutorial, we will learn how to install the Django web framework with PIP on Debian 10. We’ll go over how to create a Django application and connect it with a database.

What we need:

  • A server running Debian 10
  •  Root (or sudo) access to this server.

Getting Started

Let’s first make sure our system is up to date, for that we’ll run the following commands:

# apt update
# apt upgrade -y




Once we’re up to date we can go onto installing a few things we’ll need to actually install django, such as Python:

# apt-get install python3 python3-pip tree -y



Once done let’s go ahead and verify the version of the packages we just installed:

# pip3 -V

pip3-V


We’re now ready to install Django by running:

# pip3 install django

 
pip3installdjango



Once the install is complete let’s check on the installed version with the following command:


# django-admin --version
3.0.3


Creating a new Django project:

Let’s go into the /opt directory which is where we’ll create new projects:


# cd /opt



We’ll now run the following command to create a project named MynewDjangoProject:

# django-admin startproject MynewDjangoProject



Once this is done we’ll go ahead and run the “tree” command to see the current directory structure

# tree


It should look something like this:

djangotreestructure 






Let’s now go into the “MynewDjangoProject” folder and migrate some remaining change:

# cd MynewDjangoProject; python3 manage.py migrate


The output should look like this:

djangomigrate 

With this we now have our Django project inside /opt.


Creating a super user for Django:

We need to create a Django superuser to access the admin interface, so we’ll just do that with the following command:


# python3 manage.py createsuperuser


djangocreateuser



Starting the Django server:

The default configuration of Django does not allow for it to be accessible remotely, so to allow this we’ll need to define the server IP address in the following file by using nano:


# nano /opt/MynewDjangoProject/MynewDjangoProject/settings.py


Let’s find the following line:
ALLOWED_HOSTS = []


and define our server IP:
ALLOWED_HOSTS = ["x.x.x.x"]

Where x.x.x.x is your actual server IP;  we save and close the settings.py file after defining the IP address and we move onto starting the django server with the following command (where x.x.x.x is your server IP as defined on the previous step):

# cd /opt/MynewDjangoProject; python3 manage.py runserver 0.0.0.0:8000


The output should look like this:

Performing system checks...

System check identified no issues (0 silenced).

March 03, 2020 - 12:28:55

Django version 3.0.3, using settings 'MynewDjangoProject.settings'

Starting development server at 0.0.0.0:8000/ 

Quit the server with CONTROL-C.


Accessing Django:

If all went well we should now have a Django server listening on port 8000 so we should be able to access by going to http://your-server-ip:8000 and it should look like this:



djangofirstaccess



Now, to access the admin section you should go to http://your-server-ip:8000/admin and it should look like this:

djangoadmin

We can login using the admin credentials we created previously:
djangoadmininterface



Installing the MariaDB data base connector:

We need to be able to connect Django with MariaDB so we’ll run the following command to install the required packages to accomplish this:


# apt-get install mariadb-server python3-dev libmariadb-dev libmariadbclient-dev -y


Once this is done we’ll install the mysqlclient library using pip3:

# pip3 install mysqlclient


Once installed we’ll go into the MariaDB shell by runnning:

# mysql

Let’s set a root password:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD("somepass”);



L et’s create a database by running the following command:
  
create database mytestdb;



Let’s now see what databases are currently on the system by running:

create database mytestdb; 




The output should look like this:

MariaDB [(none)]> create database mytestdb;

Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| mytestdb           |

| performance_schema |

+--------------------+

4 rows in set (0.001 sec)




We’ll flush privileges and exit from the MariaDB shell:

flush privileges;
exit;


Configuring Django to connect to DB:

We’ll define the DB credentials in the Django settings:


# nano /opt/MynewDjangoProject/MynewDjangoProject/settings.py




Let’s find the following:

 

DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.sqlite3',

        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

    }

}



and change to:

DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.mysql',

        'OPTIONS': {

        'read_default_file': '/etc/mysql/mariadb.conf.d/50-client.cnf',

        },

    }

}




We can save and close; now we edit the MariaDB client credentials file:

# nano /etc/mysql/mariadb.conf.d/50-client.cnf

Define your MariaDB credentials as follows:

[client]
database = mytestdb
user = root
password = somepass
default-character-set = utf8 


Let’s restart MariaDB by running the following:

# systemctl restart mariadb



Let’s now change dir to the Django project  and migrate the new changes:

# cd /opt/MynewDjangoProject/; python3 manage.py migrate


Django is now configured to use MariaDB, let’s go ahead and start it:


cd /opt/MynewDjangoProject; python3 manage.py runserver 0.0.0.0:8000


Output:

Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
March 03, 2020 - 13:28:17
Django version 3.0.3, using settings 'MynewDjangoProject.settings'
Starting development server at 0.0.0.0:8000
Quit the server with CONTROL-C.

We can now open the Django web interface as we did before by going to http://your-server-ip:8000:

djangofirstaccess




Once you're done testing you can kill the Django server fro the linux cli by pressing CTRL + X

 

 

  • django, debian 10, web application framework
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

ownCloud Auto Install Script on Debian 7

Here is an easy way to install ownCloud on Debian 7 with a script, but first, for those who...

NGINX – Allow access only to certain IPs

Nginx has a nice module that not many people know about, it basically enables us...

How to configure NTP client in CentOS

What's NTP? NTP stands for Network Time Protocol, and it is an Internet protocol used to...

Initial Server Setup on CentOS 6

Here are some recommendations to setup your VPS or server when you first get it, some of these...

How To Install nginx on CentOS

What is NGINX? Nginx (pronounced "engine-x") is an open source reverse proxy server for HTTP,...