在编辑页面时实时预览页面更改。
项目描述
Wagtail Live Preview
Wagtail Live Preview 允许您在Wagtail Admin中实时查看您正在进行的页面更改。
使用React或Vue? 这对您不起作用,也不是为此设计的。此实时预览软件包是为简单的Wagtail网站设计的。
它是如何工作的
告诉它多久保存一次您正在处理的页面的快照以及多久轮询一次Live Preview的更新。
它不依赖于Wagtail的修订系统,但您可以告诉它在每 x 次保存后保存页面修订,这样您就永远不会意外地丢失工作(或者如果您只是想记录进度并可能回滚到之前的内容迭代)。
该软件包本身称为 wagtail-livepreview 以让每个人都知道这是一个针对Wagtail的特定软件包。但代码引用 livepreview 而不是 wagtail_livepreview,以避免将Wagtail功能与软件包中的内容混淆。
安装
使用以下命令安装软件包
pip install wagtail-livepreview
在 INSTALLED_APPS 中添加它,位于 'wagtail.admin' 应用程序之上
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
钩子
您可以使用通用的 Wagtail 钩子在 Live Preview 之前和之后执行操作
@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)
注意:在这些钩子中提供耗时的任务不是一个好主意,因为这些钩子可能每秒被调用一次。最好将这些任务卸载到任务运行器中。
检查视图是否为 Live Preview
您需要调整模板,以防止每秒触发您的分析。您可以使用以下方法来防止这种情况:
{% 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
模型设置
您可以为特定的页面模型禁用 Live Preview。例如,您可能有一个只有 title 字段的简单博客索引页面。或者一个重定向到另一个页面的页面。在这些情况下,您可能不想启用 Live Preview。
class YourPage(Page): # ... LIVEPREVIEW_DISABLED = True # Disable Live Preview on a per-model basis
项目详情
wagtail-livepreview-0.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fc19d1337933185575e4fb1bd6b0c785fb423d73347745048cdb2887765bdfbf |
|
MD5 | fb25c2bdd9d9c6f72b25f814c2a4ed96 |
|
BLAKE2b-256 | 8e68ee43efc07b1adaa78c837fee4a99dfcce59a5062f8ed6ee2884e22c40670 |