How to make a .bat file (script file) to run Django server in virtual environment quickly .

Whenever , We have to run our Django server , we have to go to the path , where our Django project is residing , and give the command to run our Django development server.

You have to set up your path and give the command to run server each time in the terminal, it’s like repeating same task again and again.

With few line of scripts you can run Django server , in virtual environment on double clicking the script file (.bat file).

Format of .bat file (Script file) , which we have to made :-

cd "location of manage.py file in Django Project"                                -- 1st Part

Contents of activate.bat file in scripts folder of virtual environment folder    -- 2nd part

.\manage.py runserver                                                            -- 3rd Part

Above , we will take content of “activate.bat” file , from the scripts folder present in virtual environment folder of our project.

We will integrate , above 3 parts into a single file , to make a .bat file to run our Django Server in virtual environment.

Location of manage.py file of our Django Project (1st part) :-

When you will open your folder of Project where , manage.py file is present , copy that path like –

Here my location is – C:\Desktop\Work\working_projects\top_\Book

You have to keep your manage.py file location of project in your PC.

Content of activate.bat file (2nd Part)

If , You have not made “Virtual Environment” in your project than make virtual environment .

In Below , you could also see ,

My Project name is – > “Book”.

My Virtual Environment name is – > “okk”

You have to go in scripts folder of your virtual environment folder in your Project like , In my case , I have to go in okk folder like –

Now go in Scripts folder – > Activate.bat file

Copy content of Activate.bat , This file is like –

 set "VIRTUAL_ENV=C:\Desktop\working_projects\top_\okk"
 if defined _OLD_VIRTUAL_PROMPT (
 set "PROMPT=%_OLD_VIRTUAL_PROMPT%"
 ) else (
 if not defined PROMPT (     set "PROMPT=$P$G" ) if not defined VIRTUAL_ENV_DISABLE_PROMPT (     set "_OLD_VIRTUAL_PROMPT=%PROMPT%" )
 )
 if not defined VIRTUAL_ENV_DISABLE_PROMPT (
 set "ENV_PROMPT=" if NOT DEFINED ENV_PROMPT (     for %%d in ("%VIRTUAL_ENV%") do set "ENV_PROMPT=(%%~nxd) " ) ) set "PROMPT=%ENV_PROMPT%%PROMPT%"
 )
 REM Don't use () to avoid problems with them in %PATH%
 if defined _OLD_VIRTUAL_PYTHONHOME goto ENDIFVHOME
 set "_OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME%"
 :ENDIFVHOME
 set PYTHONHOME=
 REM if defined _OLD_VIRTUAL_PATH (
 if not defined _OLD_VIRTUAL_PATH goto ENDIFVPATH1
 set "PATH=%_OLD_VIRTUAL_PATH%"
 :ENDIFVPATH1
 REM ) else (
 if defined _OLD_VIRTUAL_PATH goto ENDIFVPATH2
 set "_OLD_VIRTUAL_PATH=%PATH%"
 :ENDIFVPATH2
 set "PATH=%VIRTUAL_ENV%\Scripts;%PATH%"

Making of .bat file (script file) to run Django server in virtual environment

Just integrate 1st Part , 2nd Part & 3rd Part like –

cd C:Folder1\Desktop\Work\working_projects\top_\Book          #1st Part

set "VIRTUAL_ENV=C:\Desktop\working_projects\top_\okk"        #2nd Part



if defined _OLD_VIRTUAL_PROMPT (

    set "PROMPT=%_OLD_VIRTUAL_PROMPT%"

) else (

    if not defined PROMPT (

        set "PROMPT=$P$G"

    )

    if not defined VIRTUAL_ENV_DISABLE_PROMPT (

        set "_OLD_VIRTUAL_PROMPT=%PROMPT%"

    )

)

if not defined VIRTUAL_ENV_DISABLE_PROMPT (

    set "ENV_PROMPT="

    if NOT DEFINED ENV_PROMPT (

        for %%d in ("%VIRTUAL_ENV%") do set "ENV_PROMPT=(%%~nxd) "

    )

    )

    set "PROMPT=%ENV_PROMPT%%PROMPT%"

)



REM Don't use () to avoid problems with them in %PATH%

if defined _OLD_VIRTUAL_PYTHONHOME goto ENDIFVHOME

    set "_OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME%"

:ENDIFVHOME



set PYTHONHOME=



REM if defined _OLD_VIRTUAL_PATH (

if not defined _OLD_VIRTUAL_PATH goto ENDIFVPATH1

    set "PATH=%_OLD_VIRTUAL_PATH%"

:ENDIFVPATH1

REM ) else (

if defined _OLD_VIRTUAL_PATH goto ENDIFVPATH2

    set "_OLD_VIRTUAL_PATH=%PATH%"

:ENDIFVPATH2



set "PATH=%VIRTUAL_ENV%\Scripts;%PATH%"

.\manage.py runserver                                    #3rd Part

Now save this file as .bat extension , and when you double click on this file , you will find that your server will automatically start through terminal.