Runserver Django Apps for Lazy Developer (with Batch Script)
If you were like me, you might have been tired of typing
python manage.py runserver 192.168.100.1:8000 over and over, to see how the application worked in your local network. Therefore it occurred to me, to write a simple batch script to make lazy people like me have easier life. So here we go!
The purpose of our batch script is to runserver our django app with only a double click. And, what we need are:
- IP address, we need to get our computer server’s IP address with batch script. We are not gonna use 127.0.0.1 since we need to access the app from different devices.
- The location of our app (the file path), we need the app location so that we don’t run a wrong app, right?
- The location of our virtual environment and how to activate it. I usually name the folder
venvand put it in the root of the app folder.
- Runserver command, which is
python manage.py runserver ip_address:80. We use port 80 because I will be too lazy to write the port on browser (lol).
So with those goals in mind, here is the full script:
@ECHO OFF, is to disable the echoing so that it will not display the content of the batch script.
The purpose of the command in this line is to get IP address from ipconfig command and then set it to a variable
FOR /F “delims=: tokens=2” %%a in (‘ipconfig ^| find “IPv4”’) do set _IPAddress=%%a
There are actually three commands in this line.
cd /d %~dp0 & “venv\Scripts\activate” & python manage.py runserver %_IPAddress%:80
cd /d %~dp0, is to direct us to where the batch file is located, which is the root of our app.
“venv\Scripts\activate”, is to activate the virtual environment. Since we’re already directed to the root of our app, we don’t need to write the full path.
- after the virtual environment activated, we run the server with
python manage.py runserver %_IPAddress%:80. While
%_IPAddress%is the variable where we put our IP address.
The last line
cmd /k is to prevent the command prompt window from closing.