【问题标题】:Change boolean value and refresh page Wagtail更改布尔值并刷新页面 Wagtail
【发布时间】:2020-10-20 13:03:19
【问题描述】:

我在 wagtail 管理员的页面列表中添加了一个自定义按钮。现在我想添加一个函数,一旦管理员单击新的自定义按钮并重新加载页面,该函数就会将该特定页面中的布尔值从 false 设置为 true,反之亦然。

#admin.py
class ProductButtonHelper(ButtonHelper):

    # Define classes for our button, here we can set an icon for example
    view_button_classnames = ['button-small', 'icon', 'icon-site'] 

    def view_button(self, obj):
        # Define a label for our button
        text = 'Objavi na Vojvodjanski'
        return {
            'url': obj,#here i think the url should be the same page that you're on 
            'label': text,
            'classname': self.finalise_classname(self.view_button_classnames),
            'title': text,
        }

    def get_buttons_for_obj(self, obj, exclude=None, classnames_add=None, classnames_exclude=None):
        """
        This function is used to gather all available buttons.
        We append our custom button to the btns list.
        """
        btns = super().get_buttons_for_obj(obj, exclude, classnames_add, classnames_exclude)
        if 'view' not in (exclude or []):
            btns.append(
                self.view_button(obj)
            )
        return btns

谢谢

【问题讨论】:

    标签: python django wagtail


    【解决方案1】:

    好的,如果有人偶然发现同样的问题,这就是我想出的。

    #urls.py
    
    urlpatterns = urlpatterns + [
        
        ...
    
        path('test/confirm/<int:pk>', confirm, name='confirm-page'),
    ]
    
    
    #view.py
    
    def confirm(request, pk):
        inst = BlogPage.objects.get(id=pk)
        inst.ne_propustite = not inst.ne_propustite
        inst.save()
        #Return to previous url
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
    
    

    admin.py 与问题中的相同。

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 2010-12-26
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多