Django, install this framework in Ubuntu easily

About Django

In the next article we are going to take a look at Django. This is a high-level Python web framework that encourages rapid development and clean, pragmatic application design. It takes care of much of the complications of web development, allowing us to focus on writing our application without reinventing the wheel. Is free and open source.

Django us allows you to create web applications easily and quickly with less coding. It is a fast and secure framework written using the Python programming language. In this short tutorial, we will see how to install this framework on Ubuntu 17.10. Although this will work on other systems based on Debian / Ubuntu and its derivatives like Linux Mint.

Install the Django Web Framework on Ubuntu

We can install Django on Ubuntu using two methods:

  • Using the official repositories from Ubuntu;
  • Using pip (which is the recommended method and which I will use in this article).

Install Django Web Framework on Ubuntu using official repositories

Django is available from the official Ubuntu repositories. We can install it using from the terminal (Ctrl + Alt + T) the commands:

sudo apt update && sudo apt install python-django

With this we will have already installed this framework in Ubuntu. The only problem with the installation from the official Ubuntu repositories is that the version in the official one will be lower than the official version of Django.

Install Django Web Framework on Ubuntu using pip

This is officially recommended by the project team. We can get the latest stable version using python package manager called pip.

Install Django with Python 2

sudo pip install django

Install Django with Python 3

sudo pip3 install django

We can use Python 2 or Python 3. I am going to use Python 3 for this example.

Once the installation is complete, to check version that we have installed, we can execute:

django version

django-admin --version

As I have already said, this It is a higher version than the one in the official Ubuntu repositories. Once the installation is finished we can move on.

Basic use of Django

We are going to create a new project called entreunosyceros. To do so, run in the terminal:

django-admin startproject entreunosyceros

The above command will create a directory called «interunosyceros»In the current directory.

We are going to verify the content of this directory. To do so, run:

django directory

ls entreunosyceros/

As you can see from the output above, there is a script called «manage.py»And another directory called«interunosyceros«. The second directory 'interunosyceros'we will have the actual code.

Now, we are going to move to the first directory 'entreunosyceros':

cd entreunosyceros/

Start the database

Then run the following command to start the database:

Django start database

python3 manage.py migrate

Note: If you are using Python 2 or earlier, you must use "python manage.py migrate" without quotes.

Create administrative user

Then we will need to create an administrative user. To do so, run:

django create user

python3 manage.py createsuperuser

Write the username (leave it blank to use the current username), an email and the password, which cannot be only numeric.

Modifying ALLOWED_HOSTS in the configuration

Before we can test our application, we must modify one of the directives in the framework configuration. Open the configuration file by typing in the terminal:

django allowed hosts

nano ~/entreunosycero/entreunosyceros/settings.py

In my case I have used entreunosyceros as the name of the project. Let each one adapt it to what they have written.

Inside the file, we will look for the ALLOWED_HOSTS directive. It defines a whitelist of addresses or domain names that can be used to connect to the framework. Any incoming request with a host header that is not on this list will throw an exception. We will have to configure this to avoid a security vulnerability.

In the brackets, list the IP addresses or domain names that are associated with our framework. Each item must appear in entries separated by a comma. If we want to use requests for an entire domain and any subdomains, adds a period to the beginning of the entry.

Start the server

Finally, run the following command to start the Django development server. I use the IP 0.0.0.0, but this is just an example.

letting start console server

python3 manage.py runserver 0.0.0.0:8000

The Django server will start. To stop the server, press CTRL + C.

Access the server's web page

Open your web browser and navigate to http://Dirección IP:8000.

Django server running

If we see a screen like the previous one, the framework will be working correctly. For access the server administration page, we will write as URL http://Dirección IP:8000/admin.

We will have to use previously created username and password.

This is what my admin page of this framework looks like.

Django admin page

Django is ready to use. For more details, see the official documentation of the project.


3 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Raul said

    Great, it works great, the only thing that I couldn't modify the settings files in the terminal, but I modified it in the text editor.

  2.   Juan said

    Hello, my problem is that the login page sees a white box for login without the blue part "django" is like it does not have the format, just like when you entered the administration site, everything looks messy without color or format.

  3.   edward castle said

    Thanks for the support.