实时预览页面更改。
wagtail-livepreview的Python项目详细描述
Wagtail实时预览
wagtail实时预览允许您在wagtail管理中查看页面更改。
使用react或vue?这对您不起作用,也不是为您设计的。这个实时预览包是为简单的摇摆网站设计的。
工作原理
告诉它保存正在处理的页面快照的频率,以及在实时预览中轮询更新的频率。
它不会在wagtails修订系统上运行notpiggy back,但是您可以告诉它每隔x次保存就保存一个页面修订,这样您就不会意外地丢失您的工作(或者如果您只想记录您的进度,并可能返回到以前的内容迭代)。
这个包本身被称为wagtail-livepreview,让每个人都知道这是一个特定于wagtail的包。但是代码引用livepreview而不是wagtail_livepreview,以避免混淆wagtail特性和包中的内容。
安装
安装软件包时使用:
pip install wagtail-livepreview
将其添加到上方的INSTALLED_APPS应用程序中:
INSTALLED_APPS = [ # ... 'livepreview', # ... 'wagtail.admin', ]
将{% load livepreview_tags %}添加到base.html模板中。并在base.html中的</body>标记正上方添加{% livepreview_js %}:
{% load static wagtailuserbar livepreview_tags %} <!DOCTYPE html> <html class="no-js" lang="en"> <head> ... </head> <body class="{% block body_class %}{% endblock %}"> ... {% livepreview_js %} </body> </html>
您需要应用迁移:
python manage.py migrate
挂钩
您可以在实时预览之前和之后使用通用摇尾钩执行操作:
@hooks.register('after_live_preview_save') def after_live_preview_save(request, page): """Event to happen before the live preview is served.""" print(page.id) @hooks.register('before_live_preview_save') def before_live_preview_save(request, page): """Event to happen after the live preview is served.""" print(page.id)
注意:在这些钩子中提供一个进程密集型任务是不好的,因为这些钩子可能会以每秒一次的频率被调用。最好将这些钩子中的任务卸载到任务运行器。
检查视图是否为实时预览
你需要调整你的模板,这样你就不会每秒都触发你的分析。您可以使用:
{% if not livepreview %} .. analytics in here {% else %} <div id="warning">This is a live preview</div> {% endif %}
您还可以在模板中使用{{ request.livepreview }}检查request。
设置
您可以应用一些全局设置:
# base settings.py # How often (in milliseconds) should the livepreview check for page updates? Default is 1000ms. LIVEPREVIEW_TIMEOUT = 1000 # If you'd like to turn on auto-revision saving every x number of Live Preview saves, set this as True. Default is False. LIVEPREVIEW_SAVE_AS_REVISIONS = False # How many Live Preview saves should happen before a new revision is automatically saved? Default is 10. Requires LIVEPREVIEW_SAVE_AS_REVISIONS = True. LIVEPREVIEW_SAVE_REVISION_COUNT = 10 # Render Live Previews into a temporary file, and attempt to serve that file. Default is true. # If True, LIVEPREVIEW_TIMEOUT can be as low as 250ms. # If False, the minimum LIVEPREVIEW_TIMEOUT is 1000ms. LIVEPREVIEW_USE_FILE_RENDERING = True
型号设置
您可以禁用特定页面模型的实时预览。例如,您可能有一个简单的blog索引页,其中只有一个title字段。或者重定向到另一个页面的页面。在这些情况下,您可能不希望启用实时预览:
class YourPage(Page): # ... LIVEPREVIEW_DISABLED = True # Disable Live Preview on a per-model basis