Siner's Blog


Start Django with Virtualenv

2019/01/12


This post is based on Ubuntu 16.04.


Here's what you need to configure a Django project using virtualenv.

  • pip
  • 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.

python_virtualenv

Now, let's start a Django project with virtualenv!


1) Install pip

Follow the command below to install pip.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py

image


2) Install virtualenv

If you have pip installed, this time you will install virtualenv using pip.

pip install virtualenv

image


3) Create virtualenv

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. 

image


4) Activate virtualenv

source venv_for_django/bin/activate

image


5) Check python version

Make sure that the virtualenv we created is based on python3 as intended.

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.

image image


6) Install Django

Install Django using pip.

pip install django

image


7) Create Django project

In the virtualenv environment whrer Django is installed, enter the following command.

django-admin startproject myproject

If the project name appears to be invalid as shown in the screenshot below, try a different project name.

image


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.)

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.

image


9) ALLOWED_HOSTS

If you connect to host ip through port 8000 through web browser, you can see the debug screen with error DisallowedHost at /.

image

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.

image

Enter '*' in ALLOWED_HOSTS to allow access even if the host's address (ip, domain) changes.

image


10) Result

After the modification, run the runserver command again and you will see that the Django server is running normally. image