intalling django

Django is a full-featured Python web framework for developing dynamic websites and applications. Using Django, you can quickly create Python web applications and rely on the framework to do a good dea

 · 5 min read

If you wish to install Django using the Ubuntu repositories, the process is very straightforward.

First, update your local package index with apt:

  1. sudo apt update

Copy

Next, check which version of Python you have installed. 22.04 ships with Python 3.10 by default, which you can verify by typing:

  1. python3 -V

Copy

You should see output like this:

Output
Python 3.10.4

Next, install Django:

  1. sudo apt install python3-django

Copy

You can test that the installation was successful by typing:

  1. django-admin --version

Copy

Output
3.2.12

This means that the software was successfully installed. You may also notice that the Django version is not the latest stable version. To learn more about how to use the software, skip ahead to learn how to create sample project.

Install with pip in a Virtual Environment

The most flexible way to install Django on your system is within a virtual environment. We will show you how to install Django in a virtual environment that we will create with the venv module, part of the standard Python 3 library. This tool allows you to create virtual Python environments and install Python packages without affecting the rest of the system. You can therefore select Python packages on a per-project basis, regardless of conflicts with other projects’ requirements.

Let’s begin by refreshing the local package index:

  1. sudo apt update

Copy

Check the version of Python you have installed:

  1. python3 -V

Copy

Output
Python 3.10.4

Next, let’s install pip and venv from the Ubuntu repositories:

  1. sudo apt install python3-pip python3-venv

Copy

Now, whenever you start a new project, you can create a virtual environment for it. Start by creating and moving into a new project directory:

  1. mkdir ~/newproject
  2. cd ~/newproject

Copy

Next, create a virtual environment within the project directory using the python command that’s compatible with your version of Python. We will call our virtual environment my_env, but you should name it something descriptive:

  1. python3 -m venv my_env

Copy

This will install standalone versions of Python and pip into an isolated directory structure within your project directory. A directory will be created with the name you select, which will hold the file hierarchy where your packages will be installed.

To install packages into the isolated environment, you must activate it by typing:

  1. source my_env/bin/activate

Copy

Your prompt should change to reflect that you are now in your virtual environment. It will look something like (my_env)username@hostname:~/newproject$.

In your new environment, you can use pip to install Django. Regardless of your Python version, pip should just be called pip when you are in your virtual environment. Also note that you do not need to use sudo since you are installing locally:

  1. pip install django

Copy

You can verify the installation by typing:

  1. django-admin --version

Copy

Output
4.0.4

Note that your version may differ from the version shown here.

To leave your virtual environment, you need to issue the deactivate command from anywhere on the system:

  1. deactivate

Copy

Your prompt should revert to the conventional display. When you wish to work on your project again, re-activate your virtual environment by moving back into your project directory and activating:

  1. cd ~/newproject
  2. source my_env/bin/activate

Copy

Development Version Install with Git

If you need a development version of Django, you can download and install Django from its Git repository. Let’s do this from within a virtual environment.

First, let’s update the local package index:

  1. sudo apt update

Copy

Check the version of Python you have installed:

  1. python3 -V

Copy

Output
Python 3.10.4

Next, install pip and venv from the official repositories:

  1. sudo apt install python3-pip python3-venv

Copy

The next step is cloning the Django repository. Between releases, this repository will have more up-to-date features and bug fixes at the possible expense of stability. You can clone the repository to a directory called ~/django-dev within your home directory by typing:

  1. git clone git://github.com/django/django ~/django-dev

Copy

Change to this directory:

  1. cd ~/django-dev

Copy

Create a virtual environment using the python command that’s compatible with your installed version of Python:

  1. python3 -m venv my_env

Copy

Activate it:

  1. source my_env/bin/activate

Copy

Next, you can install the repository using pip. The -e option will install in “editable” mode, which is necessary when installing from version control:

  1. pip install -e ~/django-dev

Copy

You can verify that the installation was successful by typing:

  1. django-admin --version

Copy

Output
4.0.4.2

Again, the version you see displayed may not match what is shown here.

You now have the latest version of Django in your virtual environment.

Creating a Sample Project

With Django installed, you can begin building your project. We will go over how to create a project and test it on your development server using a virtual environment.

First, create a directory for your project and change into it:

  1. mkdir ~/django-test
  2. cd ~/django-test

Copy

Next, create your virtual environment:

  1. python3 -m venv my_env

Copy

Activate the environment:

  1. source my_env/bin/activate

Copy

Install Django:

  1. pip install django

Copy

To build your project, you can use django-admin with the startproject command. We will call our project djangoproject, but you can replace this with a different name. startproject will create a directory within your current working directory that includes:

  1. A management script, manage.py, which you can use to administer various Django-specific tasks.
  2. A directory (with the same name as the project) that includes the actual project code.

To avoid having too many nested directories, however, let’s tell Django to place the management script and inner directory in the current directory (notice the ending dot):

  1. django-admin startproject djangoproject .

Copy

To migrate the database (this example uses SQLite by default), let’s use the migrate command with manage.pyMigrations apply any changes you’ve made to your Django models to your database schema.

To migrate the database, type:

  1. python manage.py migrate

Copy

You will see output like 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 auth.0012_alter_user_first_name_max_length... OK
 Applying sessions.0001_initial... OK

Finally, let’s create an administrative user so that you can use the Djano admin interface. Let’s do this with the createsuperuser command:

  1. python manage.py createsuperuser

Copy

You will be prompted for a username, an email address, and a password for your user.

Modifying ALLOWED_HOSTS in the Django Settings

To successfully test your application, you will need to modify one of the directives in the Django settings.

Open the settings file by typing:

  1. nano ~/django-test/djangoproject/settings.py

Copy

Inside, locate the ALLOWED_HOSTS directive. This defines a list of addresses or domain names that may be used to connect to the Django instance. An incoming request with a Host header that is not in this list will raise an exception. Django requires that you set this to prevent a certain class of security vulnerability.

In the square brackets, list the IP addresses or domain names that are associated with your Django server. Each item should be listed in quotations, with separate entries separated by a comma. If you want requests for an entire domain and any subdomains, prepend a period to the beginning of the entry:

~/django-test/djangoproject/settings.py

ALLOWED_HOSTS = ['your_server_ip_or_domain', 'your_second_ip_or_domain', . . .]

Copy

When you are finished, save the file and exit your editor.

Testing the Development Server

Once you have a user, you can start up the Django development server to see what a fresh Django project looks like. You should only use this for development purposes. When you are ready to deploy, be sure to follow Django’s guidelines on deployment carefully.

Before you try the development server, make sure you open the appropriate port in your firewall. If you followed the initial server setup guide and are using UFW, you can open port 8000 by typing:

  1. sudo ufw allow 8000

Copy

Start the development server:

  1. python manage.py runserver your_server_ip:8000

Copy

Visit your server’s IP address followed by :8000 in your web browser:

http://your_server_ip:8000

You should see something that looks like this:

Django public page

To access th



Satyendra Kumar

Today i learn intro

No comments yet.

Add a comment
Ctrl+Enter to add comment