Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

python - How to automatically reload Django when files change?

How to automatically monitor .py, .js and other source code files to restart a Django (or any other for that matter) application and refresh the browser when the source changes? This is possible in Rails using guard, in JS apps using grunt-contrib-watch and the accompanying livereload browser plugin. How can I do it for Python web apps such as Django?

I start my Django server with

foreman start

this is my Procfile:

web: newrelic-admin run-program gunicorn app.wsgi

as suggested by the Heroku/Newrelic docs or the usual

python manage.py runserver

The runserver method does restart the server on .py source changes, but not the browser and doesn't watch other files - I could run guard alongside it, but then I have two processes I have to take care of, whereas grunt or rake offer unified interfaces. I'm wondering what is the recommended way of doing this among Python developers?

I could not find any detailed, comprehensive documentation on this - only incomplete discussions here and there.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need a browser extension to accomplish auto refreshes. Take a look at https://github.com/tjwalch/django-livereload-server.

I posted a more extensive answer about this at https://stackoverflow.com/a/36961244/2950621

It works by using a manage.py command (server) to monitor your .js and other static files. The server sends a signal to the browser via websockets. There is some client-side code injected on every page. The injected code responds to the signal and refresh the browser.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...