Hello Django Developer.
In this article, we are going to learn about some fantastic stuff that will reduce your overhead with setting up a Django application or any other python application.
Django framework offers plenty of classes you can inherit to help you rapidly develop a robust application from scratch. That's why I love it, but you know, what is the thing that gives me boredom while developing an awesome application.
You catch it right, setting up a virtual environment, installing and running those boarding and lengthy commands every time.
Let's have a look at the typical Django project installation and setup process.
python -m venv venv
source venv/scripts/activate
pip install django pillow
django-admin startproject core .
django-admin startapp app
pip freeze > requirements.txt
But what is if I would say you can write this complete set of code in just one line, like this:-
./make.sh create
Isn't it was the mind blowing stuff ?
In this article, we will throughout that boring step and automate the Django project setup using Bash Script.
We will create a bash script that will help you out with setting up your Django project installation process in no time. You will have to run a single command and your project will be cooked for development.
Following are the step that you often use while developing a Django project:
- Creating virtual environment
- Activating virtual environment
- Installing Django Dependencies
- Running server
- Making Migration
- Creating Superuser
- And in the end, if you want to destroy your project.
Now just create a file in your current directory and named it as make.sh and write down the following line of code in it.
# This tells the system that the commands that follow are to be executed by the Bourne shell.
# It's called a shebang because the # symbol is called a hash,
# and the ! symbol is called a bang.
#!/bin/bash/
#Developed by:- Code-Material
COMMAND=$1 # this line will take the first argument
# and store it in the COMMAND variable
# validate the COMMAND value
if [ "$COMMAND" == "" ]; then
echo "Enter a command..."
exit 1
fi
In bash script positional argument are taken as $1, $2, $3...$n.
So if you run this file as ./make.sh command, then 'command' will be stored in the COMMAND variable. In the next line we are validating this variable, is it a command or a blank string.
Now head off toward our firsts operation that is the creation of a virtual environment, installation dependencies and project creation.
Write down the following line just below the above lines:-
if [ "$COMMAND" == "create" ]; then
echo "Creating Django Application Setup..."
# create virtual environment
python -m venv venv
# activate virtual environment
source venv/scripts/activate
echo "Installing basic dependencies..."
pip install django pillow
# create project
django-admin startproject core .
# create app
django-admin startapp app
# create requirements file
pip freeze > requirements.txt
fi
./make.sh create
Note:-
If you are unable to run ./make.sh file in Linux shell. So make sure you have the execution permission for the file. You can give permission using below command.
chmod 755 ./make.sh
if [ "$COMMAND" == "run" ]; then
echo "Running Django Server..."
python manage.py runserver
fi
if [ "$COMMAND" == "venv" ]; then
echo "Activating virtual environment..."
source venv/scripts/activate
fi
if [ "$COMMAND" == "migrate" ]; then
echo "Migration process running..."
python manage.py makemigrations
python manage.py migrate
fi
if [ "$COMMAND" == "superuser" ]; then
echo "Creating Superuser..."
python manage.py create superuser
fi
if [ "$COMMAND" == "destroy" ]; then
echo "Are you sure you want to destroy the application..."
read DESTROY_COMMAND
DESTROY_COMMAND="${DESTROY_COMMAND,,}"
if [ "$COMMAND" == "yes" || "$COMMAND" == "y" ]; then
echo "Deleting the entire django project..."
fi
fi
You are done! You have successfully set up your dinner table for the Django application. If you want to make it look more batter so here is the full code that is fully garnished with some mad stuff. I think doing it will be worth it for you with your Django development journey.
#!/bin/bash/
#Developed by:- Code-Material
# Django Project Setup Script
COMMAND=$1
if [ "$COMMAND" == "" ]; then
echo "Enter a command..."
exit 1
fi
if [ "$COMMAND" == "create" ]; then
echo "Creating Django Application Setup..."
python -m venv venv
source venv/scripts/activate
echo "Installing basic dependencies..."
pip install django pillow
django-admin startproject core .
django-admin startapp app
pip freeze > requirements.txt
echo "Setup Completed..."
echo ""
echo ".........."
echo ".........."
echo ".........."
echo ".........."
echo ".........."
echo ".........."
echo ".........."
echo ".........."
echo ".........."
echo ".........."
echo "Happy Coding...😊"
echo "Thanku For Reading Code Material Blog..."
echo ".........."
echo ".........."
echo ".........."
echo ".........."
echo ".........."
echo "run the below command to activate the server django default server..."
echo ". ./run.sh run"
fi
if [ "$COMMAND" == "run" ]; then
echo "Running Django Server..."
python manage.py runserver
fi
if [ "$COMMAND" == "venv" ]; then
echo "Activating virtual environment..."
source venv/scripts/activate
fi
if [ "$COMMAND" == "migrate" ]; then
echo "Migration process running..."
python manage.py makemigrations
python manage.py migrate
fi
if [ "$COMMAND" == "superuser" ]; then
echo "Creating Superuser..."
python manage.py create superuser
fi
if [ "$COMMAND" == "destroy" ]; then
echo "Are you sure you want to destroy the application..."
read DESTROY_COMMAND
DESTROY_COMMAND="${DESTROY_COMMAND,,}"
if [ "$COMMAND" == "yes" || "$COMMAND" == "y" ]; then
echo "Deleting the entire django project..."
rm -rf venv, core, app
rm -r migrate.py, rm -r requirements.txt
fi
fi
I hope this article has helped you better understand Automating Django project setup using a bash script and how they work together.
Now that you can change the script and manipulate it as per your choice of setup.
If you stuck with static file error in django application you can checkout this article for solution:-
Thanku for reading the complete Article.
2 Comments
You have written an excellent blog. I learned something new from your Blog. Keep sharing valuable information. WDP Technologies
ReplyDeleteThanku for you valuable comment. Keep supporting
DeleteIf you have any doubt, Let me Know
Emoji