Skip to content

Instantly share code, notes, and snippets.

@aswinjose89
Created April 26, 2025 10:12
Show Gist options
  • Save aswinjose89/48a932721f9a1e4b66dbffa3a6473be8 to your computer and use it in GitHub Desktop.
Save aswinjose89/48a932721f9a1e4b66dbffa3a6473be8 to your computer and use it in GitHub Desktop.
A shell script to create a clean Django project structure with auto app creation, port management, and conda environment checking.
#!/bin/bash
# Ask for project name
read -p "Enter your Django project name: " project_name
# Check if conda environment is activated
if [ -z "$CONDA_DEFAULT_ENV" ]; then
echo "❌ Error: No conda environment activated."
echo "πŸ”Ή To create a new environment, you can run:"
echo " conda create --name your_env_name python=3.11"
echo "πŸ”Ή Then activate it using:"
echo " conda activate your_env_name"
echo "Please activate a conda environment before running this script."
exit 1
fi
# Install Django if not installed
pip show django > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Installing Django..."
pip install django
fi
# Create outer project folder
mkdir "$project_name"
cd "$project_name" || exit
# Create Django project inside (with . to avoid nested folders)
django-admin startproject "$project_name" .
echo "βœ… Django project '$project_name' created with clean structure."
# Ask for app name
read -p "Enter your Django app name (leave blank to skip app creation): " app_name
if [ -n "$app_name" ]; then
# Create app inside the project folder
python manage.py startapp "$app_name"
echo "βœ… Django app '$app_name' created inside $project_name/."
# Add 'projectname.appname' into INSTALLED_APPS
settings_file="$project_name/settings.py"
if grep -q "INSTALLED_APPS" "$settings_file"; then
sed -i "/INSTALLED_APPS = \[/a \ '$app_name'," "$settings_file"
echo "βœ… Added '$project_name.$app_name' to INSTALLED_APPS in $settings_file."
else
echo "⚠️ Couldn't find INSTALLED_APPS in $settings_file. Please add '$project_name.$app_name' manually."
fi
else
echo "ℹ️ No app name provided. Skipping app creation."
fi
# Ask if user wants to run migrations now
read -p "Do you want to run initial Django migrations now? (y/n): " migrate_now
if [[ "$migrate_now" == "y" || "$migrate_now" == "Y" ]]; then
python manage.py migrate
echo "βœ… Django migrations completed."
else
echo "⚠️ Skipped migrations. You can run later with: python manage.py migrate"
fi
# Ask if user wants to create a superuser now
read -p "Do you want to create a Django superuser now? (y/n): " create_superuser
if [[ "$create_superuser" == "y" || "$create_superuser" == "Y" ]]; then
python manage.py createsuperuser
echo "βœ… Superuser created."
else
echo "⚠️ Skipped superuser creation. You can create later with: python manage.py createsuperuser"
fi
# Ask if user wants to run the server now
read -p "Do you want to run the Django development server now? (y/n): " runserver_now
if [[ "$runserver_now" == "y" || "$runserver_now" == "Y" ]]; then
# Ask for custom port
read -p "Enter the port number to run server (press Enter for default 8000): " port_number
# Default to 8000 if no input
if [ -z "$port_number" ]; then
port_number=8000
fi
echo "πŸš€ Starting Django server on port $port_number ..."
python manage.py runserver 0.0.0.0:"$port_number"
else
echo "⚠️ Skipped running server. You can start later with: python manage.py runserver"
fi
# Final message
echo "---------------------------------"
echo "🎯 SETUP COMPLETED!"
echo "Project Structure:"
echo "$project_name/"
echo "β”œβ”€β”€ manage.py"
echo "β”œβ”€β”€ $project_name/"
if [ -n "$app_name" ]; then
echo "β”‚ └── $app_name/"
fi
echo "---------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment