Created
October 7, 2023 22:45
-
-
Save pitvfx/7efcd91ab6ce081a22cd33e990eb4f5d to your computer and use it in GitHub Desktop.
linux django start up script
This file contains 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 | |
# Check if pyenv is installed, else exit with an error message | |
if ! command -v pyenv &> /dev/null; then | |
echo "Error: pyenv is not installed. Please install pyenv and try again." | |
exit 1 | |
fi | |
# Check if pipenv is installed, else exit with an error message | |
if ! command -v pipenv &> /dev/null; then | |
echo "Error: pipenv is not installed. Please install pipenv and try again." | |
exit 1 | |
fi | |
# Ask the user which Python version to use | |
read -p "Enter the Python version to use (press Enter for default): " python_version | |
# Ask for the project name | |
read -p "Enter the Django project name: " project_name | |
# Create a directory for the project | |
mkdir $project_name | |
cd $project_name | |
# Create an empty Pipfile | |
touch Pipfile | |
# Install python via pipenv if a version number is given | |
if [ -n "$python_version" ]; then | |
pipenv install --python $python_version | |
fi | |
# Install Django using pipenv and django-debug-toolbar for development | |
pipenv install django | |
pipenv install django-debug-toolbar --dev | |
# Create a Django project | |
pipenv run django-admin startproject $project_name . | |
# Ask for the app name and create the app | |
read -p "Enter the Django app name: " app_name | |
pipenv run python manage.py startapp $app_name | |
# Add the app to project settings | |
echo -e "\n# Add $app_name to installed apps\nINSTALLED_APPS += ['$app_name',]" >> $project_name/settings.py | |
# Apply migrations | |
pipenv run python manage.py migrate | |
# Ask for superuser name and create one with password "password" | |
read -p "Enter the superuser name: " superuser_name | |
export DJANGO_SUPERUSER_PASSWORD=password | |
pipenv run python manage.py createsuperuser --username $superuser_name --email $superuser_name@localhost --noinput | |
echo "Superuser $superuser_name created with password 'password'." | |
# Create a runserver.sh file with the command to run the server | |
echo "pipenv run python manage.py runserver" > runserver.sh | |
chmod +x runserver.sh | |
# Execute the runserver.sh script in another terminal window | |
x-terminal-emulator -e "./runserver.sh" & | |
sleep 1 # wait for the server to start | |
# start webbrowser | |
xdg-open http://localhost:8000/admin/ | |
#this will exit the shell and activate the virtual environment | |
pipenv shell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment