Runserver Django Apps for Lazy Developer (with Batch Script)

Photo by Steinar Engeland on Unsplash

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!

1. Goal

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 venv and 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).

2. Result

So with those goals in mind, here is the full script:

Line 1

@ECHO OFF, is to disable the echoing so that it will not display the content of the batch script.

Line 2

The purpose of the command in this line is to get IP address from ipconfig command and then set it to a variable _IPAddress.

FOR /F “delims=: tokens=2” %%a in (‘ipconfig ^| find “IPv4”’) do set _IPAddress=%%a

Line 3

There are actually three commands in this line.

cd /d %~dp0 & “venv\Scripts\activate” & python manage.py runserver %_IPAddress%:80
  1. cd /d %~dp0, is to direct us to where the batch file is located, which is the root of our app.
  2. “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.
  3. 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.

Line 4

The last line cmd /k is to prevent the command prompt window from closing.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adhi Setyatuhu

A Kotlin android developer and a full stack web developer (familiar with Django) based in Indonesia. Interested in remote jobs.