Creating a terminal command to start your Django project
Whenever starting a new Django project, I like to get a few things in order to make sure it's as easy to work on as possible. The most simple, and useful, of these is to create a terminal command I can use that does the following:
- - Sets any enviroment variables needed to run the project
- - Activates the project's virtual environment
- - Starts up python manage.py runserver so that I can view my site and get right to coding
This is a very simple process that doesn't only save time in the long run but makes bouncing between projects a piece of cake.
Please note: These commands are for Linux Bash but should work (w/ the exception of gedit) on Mac as well.
Prerequisites: A working Django project and development environment that uses virtualenv and virtualenvwrapper.
Open up your terminal and create a file in /usr/local/bin named exactly what you'd like to type in to the terminal to run your project.
ian@ubuntu:~$ cd /usr/local/bin && sudo touch MY_COMMAND_NAME && sudo gedit MY_COMMAND_NAME
You should see gedit (a simple text editor) open your new blank file (MY_COMMAND_NAME). Within it, enter the following (edited with your own values).
#!/bin/bash # This is the necessary 'Shabang', instructing that the file is a bash script. echo "Setting SECRET_KEY env. variable" export SECRET_KEY="some random characters" # In my settings.py file, I have `SECRET_KEY = os.environ.get('SECRET_KEY")`, instructing Django to fetch this from the computer's environment variables. However, I often use env. variables for email and staticfile settings; Setting these values in this file for local dev. allows me to set, but easily change, the values of such parameters when I move to production. echo "CDing to my particular web application" cd ~/Documents/Programming/MyProject # Simply enter the path to your project root dir here; the tilde (~) represents your home folder (viewable in terminal by entering `$HOME`) source /usr/share/virtualenvwrapper/virtualenvwrapper.sh # This imports the standard virtualenvwrapper commands echo "Activating virtual environment" workon MY_EXISTING_VENV_NAME # Since we have imported virtualenvwrapper commands, we are now simply activating the environment echo "Starting server" python manage.py runserver
Save the file and enter the following in the terminal to allow execution of your script.
ian@ubuntu:~$ sudo chmod 755 /usr/local/bin/MY_COMMAND_NAME
The file is marked as executable so that you have rwx permissions but other users can only rx. Close the current terminal, open a new one, and your command is now loaded!
You have two options now:
- 1. Run MY_COMMAND_NAME; this will start up the server and, when you exit, ren to the directory and terminal state from before calling the command. This is because, by default, shell commands run in a 'subshell' of your terminal.
- 2. Run . MY_COMMAND_NAME (WITH the preceding dot and space). This dot command means that your script will not be run in a subshell, but in the active shell itself; when you exit the server, your virtual environment is still active and you are in your project's root directory.
The picture below shows such a command in action. I've been working on a subscription box site, hence naming the command SUBBOX. With an alias for each of my projects, I know I can switch from BS_FUN to SUBBOX and get some real work done in a single command. Get some work done, earth slugs!
Want to work together? Send an email.
©2018 Ian Price / Red Mountain GIS