Created
April 26, 2025 10:12
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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