简体   繁体   中英

DJANGO + windows: .bat file: open a command prompt and run the commands

I have to run the following set of commands after opening a windows cmd prompt

D:
cd Dev\project\backend
.\venv\Scripts\activate
python manage.py runserver

How to write a script(.bat) file for running these commands and the runserver will stay showing the output

I tried to put these commands in django.bat and double click, but a cmd window opens and within a fraction of second it closes. Dont know what happened

Instead of activating the venv then running manage.py separately, you should run directly using the python.exe from your virtual env.

It should look like this: > D:\\Dev\\project\\backend\\venv\\Scripts\\python.exe D:\\Dev\\project\\backend\\manage.py runserver

I don't really know what you intent to use this for, but in windows you should run your Django project using IIS. Check out django-windowsauth , you can use it to run Django on IIS in few commands. https://github.com/danyi1212/django-windowsauth . Just skip adding the middleware and the authentication backend of you don't need Windows Authentication on your site.

I tried to do this recently, but had to move onto something else quickly; given your question it's a nice opportunity to return to it, and I believe I've found the answer!

From what I found on my Laptop just now, the issue is with the activate line

.\venv\Scripts\activate

On Windows, this is another .bat file, and I suspect that calling it runs in a new cmd instance, rather than the current one

So I simply copied the entire contents of activate.bat and put it into my custom script, and it worked!

@echo off
cd D:\Dev\project\backend
-> Paste entire contents of activate.bat on this line <-
.\manage.py runserver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM