代码之家  ›  专栏  ›  技术社区  ›  Mess

在wagtail代码片段中添加自定义操作按钮

  •  0
  • Mess  · 技术社区  · 6 月前

    Screenshot of default actions 我一直在尝试浏览关于如何为wagtail代码段添加自定义操作按钮的文档。到目前为止还没有运气。

    我的摇尾版本是6.1.3

    这是我的代码片段类。

    class CurrentDayForecastViewSet(SnippetViewSet):
        model = CurrentDayForecast
        menu_label = 'Current Day Forecast'
        list_display = ('forecast_title', 'forecast_date', 'town')
        search_fields = ('forecast_title', 'forecast_date', 'town')
    
        panels = [
            FieldPanel('forecast_title'),
            MultiFieldPanel([
                FieldPanel('forecast_date', classname='col6'),
                FieldPanel('town', classname='col6'),
                FieldPanel('min_temperature', classname='col6'),
                FieldPanel('max_temperature', classname='col6'),
            ], heading="Forecast Details", classname='col12'),
    
            MultiFieldPanel(
                [
                    FieldPanel('weather_condition_5AM', classname='col4'),
                    FieldPanel('wind_direction_5AM', classname='col4'),
                    FieldPanel('wind_speed_5AM', classname='col4'),
                ],
                heading='5AM',
                classname='collapsible col12'
            ),
            MultiFieldPanel(
                [
                    FieldPanel('weather_condition_12PM', classname='col4'),
                    FieldPanel('wind_direction_12PM', classname='col4'),
                    FieldPanel('wind_speed_12PM', classname='col4'),
                ],
                heading='12PM',
                classname='collapsible col12'
            ),
            MultiFieldPanel(
                [
                    FieldPanel('weather_condition_5PM', classname='col4'),
                    FieldPanel('wind_direction_5PM', classname='col4'),
                    FieldPanel('wind_speed_5PM', classname='col4'),
                ],
                heading='5PM',
                classname='collapsible col12'
            ),
            MultiFieldPanel(
                [
                    FieldPanel('weather_condition_9PM', classname='col4'),
                    FieldPanel('wind_direction_9PM', classname='col4'),
                    FieldPanel('wind_speed_9PM', classname='col4'),
                ],
                heading='9PM',
                classname='collapsible col12'
            ),
        ]
    

    默认情况下,操作下拉菜单有“编辑、复制、删除”。我需要为这个代码段添加一个自定义操作按钮,以运行一些自定义逻辑。

    如果有人能给我指出正确的方向,我将不胜感激。

    1 回复  |  直到 6 月前
        1
  •  0
  •   gasman    6 月前

    使用 register_snippet_listing_buttons 钩子。从文档(添加到 wagtail_hooks.py 任何应用程序中的文件):

    from wagtail import hooks
    from wagtail.snippets import widgets as wagtailsnippets_widgets
    
    @hooks.register('register_snippet_listing_buttons')
    def snippet_listing_buttons(snippet, user, next_url=None):
        yield wagtailsnippets_widgets.SnippetListingButton(
            'A page listing button',
            '/goes/to/a/url/',
            priority=10
        )