Runserver Django Apps for Lazy Developer (with Batch Script)

Adhi Setyatuhu
2 min readMay 19, 2021

--

Photo by Steinar Engeland on Unsplash

If your computer is connected to an access point through wifi. You can use your computer as a server that can be accessed by other devices connected to the same network. In Django you can run python manage.py runserver 192.168.1.1:8000 (assuming 192.168.1.1 is your computer ip address that is used as the server).

Since I like testing my apps on my phone and tablet simultaneously, it’s quite convenient to runserver it like that. But, doing it over and over is becoming boring and as a programmer we like to automate or simplify our work, right? So then I came to this solution — one click to run the server. Here is how I do it.

1. Goal

The goal is to create a batch script 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.

--

--

Adhi Setyatuhu

A full stack web developer (familiar with Django) from Indonesia. Available for remote jobs.