Python 文件更改时如何自动重新加载Django?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19094720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to automatically reload Django when files change?
提问by metakermit
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?
如何自动监视 .py、.js 和其他源代码文件以重新启动 Django(或任何其他与此相关的)应用程序并在源代码更改时刷新浏览器?这可以在 Rails 中使用 guard,在 JS 应用程序中使用 grunt-contrib-watch 和随附的 livereload 浏览器插件实现。如何为 Django 等 Python Web 应用程序执行此操作?
I start my Django server with
我启动我的 Django 服务器
foreman start
this is my Procfile:
这是我的 Procfile:
web: newrelic-admin run-program gunicorn app.wsgi
as suggested by the Heroku/Newrelic docs or the usual
按照 Heroku/Newrelic 文档或通常的建议
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?
该runserver
方法确实在 .py 源代码更改时重新启动服务器,但不是浏览器,也不会监视其他文件 - 我可以在它旁边运行警卫,但是我必须处理两个进程,而 grunt 或 rake 提供统一接口。我想知道 Python 开发人员推荐的这样做的方法是什么?
I could not find any detailed, comprehensive documentation on this - only incomplete discussions hereand there.
采纳答案by nmgeek
You don't need a browser extension to accomplish auto refreshes. Take a look at https://github.com/tjwalch/django-livereload-server.
您不需要浏览器扩展来完成自动刷新。看看https://github.com/tjwalch/django-livereload-server。
I posted a more extensive answer about this at https://stackoverflow.com/a/36961244/2950621
我在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.
它通过使用 manage.py 命令(服务器)来监控您的 .js 和其他静态文件。服务器通过 websockets 向浏览器发送信号。每个页面上都注入了一些客户端代码。注入的代码响应信号并刷新浏览器。
回答by stormlifter
Using python manage.py runserver
is what most use. You'll have to use another tool like: http://livejs.com/to refresh the browser itself since Django really isn't aware of it.
使用python manage.py runserver
是最有用的。您必须使用另一种工具,例如:http: //livejs.com/来刷新浏览器本身,因为 Django 确实不知道它。
回答by Hari Mahadevan
Frustrated with all the explicit refreshes, I created a browser extension, for both Firefox and Chrome, to automate this. The extension works with a Django app that you add to your app list in INSTALLED_APPS. You can find out more at the github repo.
对所有显式刷新感到沮丧,我为 Firefox 和 Chrome 创建了一个浏览器扩展程序,以自动执行此操作。该扩展程序适用于您添加到 INSTALLED_APPS 中的应用程序列表的 Django 应用程序。您可以在github repo 中找到更多信息。
Though the repo has entire source code, the extensions are also available in the respective web store. Just search for 'Django Auto Refresh'. With these, you just need to copy the app into our project's folder and include it via INSTALLED_APPS. I wanted to add a pip setup script, but haven't found the time to do it.
虽然 repo 有完整的源代码,但扩展也可以在相应的网上商店中找到。只需搜索“Django 自动刷新”。有了这些,您只需要将应用程序复制到我们项目的文件夹中并通过 INSTALLED_APPS 包含它。我想添加一个 pip 安装脚本,但还没有找到时间去做。
HTH. Apologies if this sounds like self promotion.
哈。如果这听起来像是自我推销,我们深表歉意。
回答by Fábio Gibson
Install this django app:
安装这个 Django 应用程序:
pip install django-livesync
On your django settings file add something like:
在您的 Django 设置文件中添加如下内容:
INSTALLED_APPS = (
'...',
'livesync',
'django.contrib.staticfiles',
'...',
)
MIDDLEWARE_CLASSES = (
'livesync.core.middleware.DjangoLiveSyncMiddleware',
)
Beware to register 'livesync' before 'django.contrib.staticfiles' if you are using it.
如果您正在使用它,请注意在“django.contrib.staticfiles”之前注册“livesync”。
Now, just start your development server:
现在,只需启动您的开发服务器:
python manage.py runserver
Check this out for more details: https://github.com/fabiogibson/django-livesync