问题描述
当我运行 ./manage.py runserver
时,它经常因我犯的错误而崩溃,例如复制/粘贴代码并保存,然后再进行修改或出现语法错误.然后,我必须重新运行服务器.
When I run ./manage.py runserver
, it often crashes because of mistakes I make, e.g. copy/paste code and save before modifying it or having a syntax error. Then I have to rerun the server.
是否有可能使服务器仍在运行,然后在随后的保存中重新加载,这样我就不必再次手动运行服务器了?
Is it possible to somehow have the server still running and then reload on my subsequent save so I don't have to run the server manually again?
推荐答案
为此,我使用了一个简单的bash脚本.这是您可以使用的单线纸:
I use a simple bash script for this. Here's a one-liner you can use:
$ while true; do python manage.py runserver; sleep 2; done
这将等待2秒钟,然后尝试重新启动服务器.插入任何您认为合理的值.
That will wait 2 seconds before attempting to restart the server. Insert whatever you think is a sane value.
我通常将此脚本编写为名为 runserver.sh
的shell脚本,将其放在我的项目根目录(其中包含manage.py的同一目录)中,并将其添加到gitignore中.
I usually write this as a shell script named runserver.sh
, put it in my project root (the same directory with manage.py in it) and add it to the gitignore.
while true; do
echo "Re-starting Django runserver"
python manage.py runserver
sleep 2
done
如果执行此操作,请记住要 chmod + x runserver.sh
,然后可以使用以下命令执行它:
If you do this, remember to chmod +x runserver.sh
, then you can execute it with:
./runserver.sh
使用 Ctrl-c Ctrl-c
退出.
这篇关于防止django runserver崩溃或在保存修复程序上重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!