As you know, web development is becoming more and more important among programmers. Just recognize that many services require the cloud to work. For this reason, many developers are more and more interested in making applications with cutting-edge technologies for their applications. So, today, I will show you how to install Django in Ubuntu 18.04.
Django is a framework for web development that uses Python as the programming language. It is very popular because it can make sites and web applications of high complexity with reasonable times and works very well with high user traffic.
So, let’s do it.
Install Django on Ubuntu 18.04
Actually, there are three different ways to install Django. This is due to the flexibility of Python as a programming language.
- You can install Django using pip.
- The second way is, install Django using Virtualenv.
- And the third option is to download it from the git repository.
In this tutorial, I will use the second option. So let’s get to work.
First, you need to know what is Virtualenv. Virtualenv is an application that allows you to create multiple virtual environments in Python. Each virtual environment can have its own version of the interpreter and separate configurations.
So, install it using pip.
:~$ sudo apt install python3-pip
Check the pip version installed.
:~$ pip3 --version
Next, install Virtualenv.
:~$ sudo apt install virtualenv python3-virtualenv
So, the next step is to create a new environment with Python3 by default called example
.
:~$ virtualenv --python=python3 example
Then, activate the new virtual environment.
:~$ cd example :~$ source bin/activate
Next, install Django.
:~$ pip install django==2.1.4
Note: At the time of writing this post, the latest stable version of Django is 2.1.4
So, check the installation.
:~$ django-admin --version
So, let’s create a Django project.
Create a new project
A recommended way to do projects with Django is to create a new virtual environment for each one.
In this case, I will continue with the one I just created. All that remains is to create the project itself.
:~$ django-admin startproject newproject
Explore the project folder.
:~$ cd newproject :~$ tree
Edit settings.py
file. In the Allowed hosts section add your IP.
:~$ nano newproject/settings.py
Now it’s time to raise the project with Django runserver.
:~$ python manage.py runserver 192.168.1.36:8000
Open your web browser and go to http://your_IP:8000
. You will see the Django default page. In short, Django is correctly installed.
If you want to stop the project, press CTRL + c.
Just create the administrator account for Django. To do this, type the following.
:~$ python manage.py migrate
Django will automatically create the database for the administrator account, however, it is necessary to execute and do the migrations.
Now create the admin account. You will have to set a username and password.
:~$ python manage.py createsuperuser
Run Django runserver again and access the administration panel.
:~$ python manage.py runserver 192.168.1.36:8000
Now, open your web browser and go to http://your_ip:8000/admin
. And you will see this:
Type your credentials. Next, click on Log in.
And that’s it.
Conclusion
Django is a framework written in python that is very popular and powerful. There are many popular sites that use it because of its tolerance to high levels of traffic. In addition, it stands out for its security policies.
Please share this post.