How to install Django web application framework on Debian 10

How to install Django web application framework on Debian 10

Django is an open source web application framework that helps you develop dynamic websites and applications. It is safe, fast and stable, allowing you to create complex websites with less code. There are several ways to install Django on your system. You can install it using a Debian repository, PIP or from a Git repository. You can choose any method according to your needs. Django allows you to create projects in the Python virtual environment. This way, you can create multiple Django environments in one system.

In this tutorial, we will learn how to install the Django web framework using PIP on Debian 10. We will also learn how to create a Django application and connect it to a database.

prerequisites

  • A server running Debian 10.
  • A root password is configured on your server.

getting Started

Before you begin, update your system with the latest version. You can do this by running:

apt-get update -y apt-get upgrade -y

After the system is updated, restart to apply the changes.

Install Django

Django is written in Python. Therefore, you need to have Python dependencies installed on your system to install Django. You can install all components with:

apt-get install python3 python3-pip tree -y

After installing all packages, you can verify the installed version of PIP with the following command:

pip3 -V

You should get the following output:

pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

Now you can proceed to install Django using PIP commands as follows:

pip3 install Django

After the installation is complete, check the Django version using the following command:

django-admin --version

You should see the following output:

3.0.2

At this point, Django is installed on your server. You can now proceed to the next step.

Create a Django project

Now let’s start creating a sample Django project. First, change the directory to the directory where you want to create your Django project / opt:

cd /opt

Next, run the following command to create a new Django project called Dproject:

django-admin startproject Dproject

After creating a project, you can view the directory structure of the project using the following command:

tree

You should get the following output:

.
??? Dproject
    ??? Dproject
    ?   ??? asgi.py
    ?   ??? __init__.py
    ?   ??? settings.py
    ?   ??? urls.py
    ?   ??? wsgi.py
    ??? manage.py

Next, change the directory to Dproject and use the following command to migrate the pending changes:

cd Dproject python3 manage.py migrate

After the migration has completed successfully, you will see the following output:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... 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 auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

At this point, a new Django project has been created in the / opt directory.

Create superuser for Django

Next, you will need to create a superuser for Django to access the Django admin interface. You can create it using:

python3 manage.py createsuperuser

Provide the required username, email address and password as shown below:

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

Start the Django server

By default, Django cannot be accessed from remote systems. Therefore, you will need to configure Django and define the server IP. You can do this by editing the settings.py file.

nano /opt/Dproject/Dproject/settings.py

Change the following lines:

ALLOWED_HOSTS = ['your-server-ip']

Save and close the file when you are finished. Next, use the following command to start the Django server:

cd /opt/Django python3 manage.py runserver 0.0.0.0:8000

After successfully starting the server, you should get the following output:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 07, 2020 - 13:01:23
Django version 3.0.2, using settings 'Dproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

Accessing Django applications

At this point, the Django application is up and listening on port 8000. To access it, open a web browser and enter the URL http: // your-server-ip: 8000. You should see the Django application in the following screen:

To access the Django Admin interface, open a web browser and enter the URL http: // your-server-ip: 8000 / admin. You will be redirected to the Django login page:

Django login

Provide your Django admin username, password, and click recording in Button. You should see the Django Admin interface in the following screen:

Django admin console

Install MariaDB database connector

Next, you will need to install the MariaDB database connector and other development kits to connect Django with the MariaDB database. You can install all components with:

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

Once all packages are installed, you can install the mysqlclient library using the PIP command as follows:

pip3 install mysqlclient

After the installation is complete, log in to the MariaDB shell using the following command:

mysql

Next, set the root password for MariaDB using the following command.

MariaDB [(none)]> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("newpassword");

Next, create a database for Django using:

MariaDB [(none)]> create database testdb;

You can view the database created above using:

MariaDB [(none)]> show databases;

You should get the following output:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| testdb             |
+--------------------+

Next, refresh the privileges and exit from the MariaDB shell using the following command:

MariaDB [(none)]> flush privileges; MariaDB [(none)]> exit;

Configure Django for MariaDB connection

Next, you will need to define MariaDB database credentials in Django. You can define it by editing the file settings.py:

nano /opt/Dproject/Dproject/settings.py

Find the following lines:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

And replace them with the following lines:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
        'read_default_file': '/etc/mysql/mariadb.conf.d/50-client.cnf',
        },
    }
}

Save and close the file when you are finished. Then, edit the MariaDB client credentials file as follows:

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

Define your MariaDB database credentials as follows:

[client]
database = testdb 
user = root   
password = newpassword
default-character-set = utf8

Save and close the file, then restart the MariaDB service to implement the changes:

systemctl restart mariadb

Next, change the directory to a Django project and use the following command to migrate the new changes:

cd /opt/Dproject python3 manage.py migrate

After the migration has completed successfully, you can proceed to the next step.

Test MariaDB connection

At this point, Django has been configured to connect to the MariaDB database. It’s time to test.

To do this, start the Django server with:

cd /opt/Django python3 manage.py runserver 0.0.0.0:8000

After successfully starting the Django server, you will get the following output:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 07, 2020 - 13:30:49
Django version 3.0.2, using settings 'Dproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

You can now access the Django application using the URL http: // your-server-ip: 8000. You should see the Django application on the following screen:

Django application

After testing, you can stop any type of Django server by pressing CTRL + C in the terminal.

in conclusion

Congratulations! You have successfully installed Django and connected it to the MariaDB database on your Debian 10 server. Now you can start developing web applications and apply migrations in Django applications. If you have any questions, feel free to ask me.

Source

Sidebar