This post is based on Ubuntu 16.04.
Here’s what you need to configure a Django project using virtualenv.
- virtualenv (Python >= 3.5)
- Django >= 2.0
0) Why virtualenv?
There are people who say that “Django works fine, Even if you are not using virtual environments”
Let’s assume the situation.
Without a virtual environment, you’re installing and using pip modules.
At that time, you installed the latest version of Django 1.11, using a new feature called A.
A few years later, as Django 2.2 becomes available and new features are added, the previously used feature A is no longer available.
You want to use Django 2.2 in a new project, so you upgrade Django 1.1, which was previously installed without a virtual environment, to 2.2.
At that very moment, the A feature you were using in a project that was being deployed in Django 1.11 would not work.
If you use a virtual environment, you can use multiple versions of python on the same host at the same time,
Therefore, you can use multiple versions of Django at the same time.
Now, let’s start a Django project with virtualenv!
1) Install pip
Follow the command below to install pip.
1 2 curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py
2) Install virtualenv
If you have pip installed, this time you will install virtualenv using pip.
1 pip install virtualenv
3) Create virtualenv
1 virtualenv -p python3 venv_for_django
If you do not use the -p option, it is recommended to specify the version, since virtualenv can be created based on python2.
4) Activate virtualenv
1 source venv_for_django/bin/activate
5) Check python version
Make sure that the virtualenv we created is based on python3 as intended.
1 python --version
To use Django 2.0 or later, you must have a version of python3 installed in virtualenv.
So, when you create virtualenv, put the -p python3 option.
If you do not include this option and you install a version of python2, a lower version of Django (1.11) will be installed.
6) Install Django
Install Django using pip.
1 pip install django
7) Create Django project
In the virtualenv environment whrer Django is installed, enter the following command.
1 django-admin startproject myproject
If the project name appears to be invalid as shown in the screenshot below, try a different project name.
8) Start Django project
Let’s go into the generated project and run python manage.py runserver with ip and port options in the basic command.
(I entered ip with 0.0.0.0 for external connection and entered port number 8000.)
1 python manage.py runserver 0.0.0.0:8000
You will see a warning about migrate as shown in the screenshot below, but you can ignore it right now.
We’ll talk about makemigrations and migrate later.
If you connect to host ip through port 8000 through web browser, you can see the debug screen with error DisallowedHost at /.
Since we do not have an ip on the ALLOWED_HOSTS list, we need to add 192.168.0.3, the ip we will connect to.
The ALLOWED_HOSTS setting exists in myproject/settings.py.
Enter ‘*‘ in ALLOWED_HOSTS to allow access even if the host’s address (ip, domain) changes.
After the modification, run the runserver command again and you will see that the Django server is running normally.