• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Install Django on Debian 9

Written by Admin, Updated On July 19, 2019
debian, django, python
How to Install Django on Debian 9

Django is the most popular Python web framework designed to build feature-filled web applications. Using it you can develop secure, scalable and maintainable dynamic web applications. Django can be installed in a Python virtual environment using pip. The classic thing to use Python Virtual Environment is that you can create multiple different Django Environments on a single machine without affecting other Django projects.
If you install Django into the global environment then you can install only one Django version on your computer. In this tutorial, we will show you how to install django on Debian 9 machine.

Prerequisites#

Before you start installing Django on Debian 9, you should logged in using non-root user account on your system with sudo privileges.

Installing Django on Debian 9#

Django packages are available in the official Debian repositories. It’s easiest way to install using the apt package manager but not flexible for installation in a virtual environment.

Installing Python 3 and venv#

Python 3.5 comes along with Debian 9. So you can verify using below command that Python 3 is installed:

python3 -V

The output should as given below:

Python 3.5.3

Creating virtual environment using venv module is the best and recommended way. The venv module is included in the python3-venv package. To install it type the following command:

sudo apt install python3-venv

You can create virtual environment for Django once the installation finished.

Create Virtual Environment#

First of all create a new directory and navigate in to it. It can be your home directory or any other directory where your user has read and write permissions.

Run the below command to create a new directory for your Django application and navigate into it:

mkdir django_app && cd django_app

Now, execute the following command to create a new virtual environment:

python3 -m venv venv

The above command will create a directory named venv, which includes Python binary, the standard Python library, Pip package manager and other supporting files.

To start using the virtual environment, you should activate it by running the activate script:

source venv/bin/activate

Now your path will change and it will show the name of your virtual environment (venv)

Install Django#

Now your virtual environment is activated and you can install Django using the Python package manager pip:

pip install django

You can confirm the installation and check the installed version typing using below command:

python -m django --version
2.2.1

Your Django version may be different from the version shown here.

Creating a Django Project#

Create a Django project named djangoapp using django-admin command-line utility.

django-admin startproject djangoapp

The above command will create a djangoapp directory in your current directory.

You can check the directory structure using the following command:

tree  mydjangoapp/
mydjangoapp/
|-- manage.py
|-- mydjangoapp
    |-- __init__.py
    |-- settings.py
    |-- urls.py
    |-- wsgi.py

This directory contain manage.py file which manage the project and other Django specific files about database configuration settings, routes, and settings.

Next, navigate to the djangoapp directory:

cd djangoapp

After that you should migrate the database using below command:

python manage.py migrate
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 sessions.0001_initial… OK

SQLite is the default database for Django. For production applications, you can use PostgreSQL, MariaDB, Oracle or MySQL Database.

Once the migrate process is complete you should create a administrative user account to access the Django admin interface:

python manage.py createsuperuser

This command will prompt you for username, email and a password for your user.

Username (leave blank to use 'tecnstuff'): admin
Email address: admin@tecnstuff.net
Password:
Password (again):
Superuser created successfully.

Testing the development server#

Launch the development server using the manage.py script as given below:

python manage.py runserver

It will show output as below:

Performing system checks…
System check identified no issues (0 silenced).
July 18, 2019 - 18:49:23
Django version 2.1.4, using settings 'djangoapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Now open http://127.0.0.1:8000 in your web browser and it will show you the default Django landing page:

django welcome page

You can access admin interface direct by visiting http://127.0.0.1:8000/admin/ URL.

django admin login page

Enter your username and password which you set at the administrative user create time and you will be redirect to Django admin page:

django admin dashboard

You can stop the development server by pressing Ctrl+C in terminal.

Deactivate the Virtual Environment#

When you want to stop work you can deactivate the virtual environment by typing deactivate:

deactivate

Conclusion#

You have learned how to create a Python virtual environment and install Django on your Debian 9 system. To create additional Django development environments repeat the steps given in this tutorial. If you want to learn more about Django, visit the Django documentation page.

If you have any questions or suggestion, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Curl Command in Linux with Examples
Next Article   How to Upgrade Debian 9 Stretch to Debian 10 Buster

Related Posts

  • How to Install WordPress with Nginx on Debian 11

    How to Install WordPress with Nginx on Debian 11

    March 22, 2023
  • How to Install and Use Docker on Debian 11

    How to Install and Use Docker on Debian 11

    March 10, 2023
  • How to Install MariaDB on Debian 11 Bullseye

    How to Install MariaDB on Debian 11 Bullseye

    March 8, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook