Running multiple Django projects at once: specifying the ports for your local environment
It was inevitable. Now that Django has proven to work really well for so many various projects, what do we do when we're working on two or more projects at once?
For example: I'm working on a website with one Django project, but I just started a new Django project for a sister website. I can't have both running, which makes it tough to double-check functionality since the sister site has a lot of similarity to the original site. And, yes, stopping the server in one terminal then going back to the other terminal to run python manage.py runserver
is WAY too hard.
The better way is to choose a port for this new project and there happens to be an easy way to choose it:
python manage.py runserver 8010
Just add your custom port number after the typical runserver command and you're golden.
However, we can make this even easier with a little aliasing.
In my terminal, I've made some shortcuts (aliases) so that I don't have to type a bunch of commands every time.
Here's my standard Django alias:
alias djstart='cd /Users/USERNAME/DJANGO && source venv/bin/activate && cd /Users/USERNAME/DJANGO/PROJECTFOLDER && python manage.py runserver'
We go to the parent folder where the virtual environment folder also resides. Activate the virtual environment. Change directory into the Django project folder (where manage.py and all of your app folders are). Finally, run the dev server.
Now that we know how to specify the port, we can make another custom alias for this new sister project we also want to run at the same time:
alias sisterstart='cd /Users/USERNAME/DJANGO-SISTER && source venv/bin/activate && cd /Users/USERNAME/DJANGO-SISTER/PROJECTFOLDER && python manage.py runserver 8010'
Now we just type:
sisterstart
...hit enter and we'll go into our new project, activate the new virtual environment, and then start up a dev server on port 8010.
We're good to go!