Skip to content

Instantly share code, notes, and snippets.

@acangiano
Last active March 14, 2024 18:31
Show Gist options
  • Save acangiano/5e064b4592b0db760366fe4248d5d657 to your computer and use it in GitHub Desktop.
Save acangiano/5e064b4592b0db760366fe4248d5d657 to your computer and use it in GitHub Desktop.
Utility shell script to create Django projects on Mac
# ...
djsetup() {
if [ -z "$1" ]; then
echo "Error: No project name provided."
echo "Usage: djsetup project_name"
return 1 # Exit the function with an error status
fi
~/bin/djsetup.zsh "$1" && cd ~/code/python/django/"$1"
}
# ...
#!/bin/zsh
# Check if a name was provided
if [ "$#" -ne 1 ]; then
echo "Usage: $(basename $0) project_name"
exit 1
fi
NAME=$1
# Navigate to the directory
cd ~/code/python/django
# Make a directory with the given name and enter it
mkdir "$NAME"
cd "$NAME"
# Set up a Python virtual environment
python3 -m venv .venv
# Activate the virtual environment
source .venv/bin/activate
# Install Django
python3 -m pip install django
# Start a new Django project with the given name
django-admin startproject "$NAME" .
# Output installed packages to requirements.txt
pip freeze > requirements.txt
# Create a .gitignore file with the .venv folder
echo ".venv/" > .gitignore
# Initialize git, add all files, and commit
git init
git add -A
git commit -m "Initial commit"
echo "Project $NAME setup is complete."

This is a utility shell script to speed up the process of setting up a Django project, from running multiple commands to simply running:

djsetup hello_world

WHAT THE SCRIPT WILL DO

  • Create a folder in your Django projects folder for this new project (e.g., ~/code/python/django/hello_world).
  • Create a .venv Python environment within that folder and activate it.
  • Install Django in .venv.
  • django-admin startproject your project
  • pip freeze > requirements.txt
  • Create a .gitignore file and add .venv/ to it.
  • Initialize git and make the initial commit.

IMPORTANT NOTE

This is a script for my machine, so it makes a few assumptions about the existence and location of certain folders. For instance, I assume that the script djsetup.zsh lives in the ~/bin folder and the django projects folder is located at ~/code/python/django. Adjust them according to your preference.

PREREQUISITES

Make sure you're using the zsh (check with echo $SHELL).

INSTRUCTIONS

  1. Create the file ~/bin/djsetup.zsh.
  2. Make it executable (run chmod a+x ~/bin/djsetup.zsh).
  3. Add the djsetup() function to your ~/.zshrc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment