Multiple Django projects using one virtualenv environment

Multiple Django projects using one virtualenv environment

In this tutorial, you will learn how to run two or more Django Projects in one virtual environment. This seems to be tricky and funny but has some pros and cons. Let’s say you are running two Projects A and B in one virtualenv. Project A requires 3 libraries and Project B Requires 2, but both are running in the same environment. This might slow your Django Projects because there are unwanted libraries and codes.

How to Run Two Django Projects on One Virtual Environment

For simplification, we will be creating a folder and keeping all our projects, virtualenv in it.

mkdir DjangoProjects

Virtrualenv

Install VirtualEnv for creating Python Environment for your Django Project. Open Command Line or Windows Powershell and type bellow command –

pip install virtualenv env

Once virtualenv is installed, you need to Activate it. For Activating Virtual Environment type –

Windows – . .\env\Scripts\activate

Unbuntu- . .\env\bin\activate

Install Django in VirtualEnv

After activating virtualenv, we will install django inside it.

pip install django

Create Django Projects in VirtualEnv

Now we will create Django Projects using the following command. We are going to create 2 django projects with name django1 and django2

django-admin startproject django1
django-admin startproject django2

Running Django Projects

Now its time to run both the projects. We will first Django Project – Django1 on Port no – 8000

python manage.py runserver 0.0.0.0:8000

Now our Django1 is running on http://localhost:8000/

Open the second terminal

In the second terminal, change your directory where you have both the projects and activate the virtualenv. After activating go inside django2 project and runserver on 8001 Port no

python manage.py runserver 0.0.0.0:8001

Now our Django2 is running on http://localhost:8001/

Conclusion

Basically we are running our Django project by changing their URL. If you see we have to change the port number of django1 to 8000 and django2 to 8001.

Share