跳转到主要内容

在编辑页面时实时预览页面更改。

项目描述

Wagtail Live Preview

Wagtail Live Preview 允许您在Wagtail Admin中实时查看您正在进行的页面更改。

使用React或Vue? 这对您不起作用,也不是为此设计的。此实时预览软件包是为简单的Wagtail网站设计的。

它是如何工作的

告诉它多久保存一次您正在处理的页面的快照以及多久轮询一次Live Preview的更新。

它不依赖于Wagtail的修订系统,但您可以告诉它在每 x 次保存后保存页面修订,这样您就永远不会意外地丢失工作(或者如果您只是想记录进度并可能回滚到之前的内容迭代)。

该软件包本身称为 wagtail-livepreview 以让每个人都知道这是一个针对Wagtail的特定软件包。但代码引用 livepreview 而不是 wagtail_livepreview,以避免将Wagtail功能与软件包中的内容混淆。

安装

  1. 使用以下命令安装软件包

    pip install wagtail-livepreview
  2. INSTALLED_APPS 中添加它,位于 'wagtail.admin' 应用程序之上

    INSTALLED_APPS = [
        # ...
        'livepreview',
        # ...
        'wagtail.admin',
    ]
  3. {% 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>
  4. 您需要应用迁移

    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 (11.5 MB 查看哈希值)

上传时间

支持者