本文介绍了文件如何自动重新加载Django?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何自动监视.py,.js和其他源代码文件,以重新启动Django(或任何其他的)应用程序,并在源更改时刷新浏览器?这可以在Rails中使用guard,在JS应用程序中使用grunt-contrib-watch和随附的livereload浏览器插件。我如何为Python网络应用程序(如Django)执行此操作?



我使用



<$ p $启动我的Django服务器p> foreman start

这是我的Procfile:

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

由Heroku / Newrelic文档或通常的

  python manage.py runserver 

runserver 方法将重启服务器。 py源改变,但不是浏览器,并且不看其他文件 - 我可以跟随它,但是我有两个进程我必须照顾,而grunt或rake提供统一的接口。我想知道Python开发人员推荐的方法是什么?



我找不到任何详细的全面的文档 - 只有不完整的讨论和。

解决方案

您不需要浏览器扩展来完成自动刷新。看看



它可以通过使用manage.py命令(服务器)来监视.js和其他静态文件。服务器通过Websockets向浏览器发送信号。每个页面上都注入了一些客户端代码。注入的代码响应信号并刷新浏览器。


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.

解决方案

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.

这篇关于文件如何自动重新加载Django?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-19 03:28
查看更多