向 Wagtail 仪表板添加按钮
Adding a button to Wagtail Dashboard
是否可以在顶部面板上添加一个额外的按钮(按钮),如图所示?
我在 Google 和这里没有找到任何东西。
屏幕截图上不清楚您是使用 modeladmin
还是 snippet
向用户公开此模型,但我假设是前者。
我不知道允许您直接将按钮添加到 header 的挂钩,但是模板 uses blocks 应该允许我们只覆盖这一部分。
我们可以利用模板的 resolution order 并创建优先于 /modeladmin/index.html
的 templates/modeladmin/app-name/model-name/index.html
。因此,如果您的应用程序名为 feedler
,模型名为 Entry
,请创建具有以下内容的 /modeladmin/feedler/entry/index.html
:
{% extends "modeladmin/index.html" %}
{% block header_extra %}
<a href="#">My New Button</a>
{{ block.super }}{% comment %}Display the original buttons {% endcomment %}
{% endblock %}
目前您的按钮没有太多作用。要创建将与该模型管理员交互的操作,您需要创建一些 button/url/permission helpers 和一个视图。
假设该操作正在将 objects 导出到 CSV 文件。振作起来,前面有很多代码。
在 /feedler/admin.py
中,创建 button/url/permission 助手并查看:
from django.contrib.auth.decorators import login_required
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.functional import cached_property
from django.utils.translation import ugettext as _
from wagtail.contrib.modeladmin.helpers import AdminURLHelper, ButtonHelper
from wagtail.contrib.modeladmin.views import IndexView
class ExportButtonHelper(ButtonHelper):
"""
This helper constructs all the necessary attributes to create a button.
There is a lot of boilerplate just for the classnames to be right :(
"""
export_button_classnames = ['icon', 'icon-download']
def export_button(self, classnames_add=None, classnames_exclude=None):
if classnames_add is None:
classnames_add = []
if classnames_exclude is None:
classnames_exclude = []
classnames = self.export_button_classnames + classnames_add
cn = self.finalise_classname(classnames, classnames_exclude)
text = _('Export {}'.format(self.verbose_name_plural.title()))
return {
'url': self.url_helper.get_action_url('export', query_params=self.request.GET),
'label': text,
'classname': cn,
'title': text,
}
class ExportAdminURLHelper(AdminURLHelper):
"""
This helper constructs the different urls.
This is mostly just to overwrite the default behaviour
which consider any action other than 'create', 'choose_parent' and 'index'
as `object specific` and will try to add the object PK to the url
which is not what we want for the `export` option.
In addition, it appends the filters to the action.
"""
non_object_specific_actions = ('create', 'choose_parent', 'index', 'export')
def get_action_url(self, action, *args, **kwargs):
query_params = kwargs.pop('query_params', None)
url_name = self.get_action_url_name(action)
if action in self.non_object_specific_actions:
url = reverse(url_name)
else:
url = reverse(url_name, args=args, kwargs=kwargs)
if query_params:
url += '?{params}'.format(params=query_params.urlencode())
return url
def get_action_url_pattern(self, action):
if action in self.non_object_specific_actions:
return self._get_action_url_pattern(action)
return self._get_object_specific_action_url_pattern(action)
class ExportView(IndexView):
"""
A Class Based View which will generate
"""
def export_csv(self):
data = self.queryset.all()
response = ...
return response
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
super().dispatch(request, *args, **kwargs)
return self.export_csv()
class ExportModelAdminMixin(object):
"""
A mixin to add to your model admin which hooks the different helpers, the view
and register the new urls.
"""
button_helper_class = ExportButtonHelper
url_helper_class = ExportAdminURLHelper
export_view_class = ExportView
def get_admin_urls_for_registration(self):
urls = super().get_admin_urls_for_registration()
urls += (
url(
self.url_helper.get_action_url_pattern('export'),
self.export_view,
name=self.url_helper.get_action_url_name('export')
),
)
return urls
def export_view(self, request):
kwargs = {'model_admin': self}
view_class = self.export_view_class
return view_class.as_view(**kwargs)(request)
在 /feedler/wagtail_hooks.py
中,创建并 register
ModelAdmin
:
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from .admin import ExportModelAdminMixin
from .models import Entry
class EntryModelAdmin(ExportModelAdminMixin, ModelAdmin):
model = Entry
# ...
modeladmin_register(EntryModelAdmin)
通过所有这些设置,您应该能够在上面创建的模板中使用 {% include 'modeladmin/includes/button.html' with button=view.button_helper.export_button %}
。
注意,如果您要列出页面,则需要从 PageAdminURLHelper 继承子类,而不是 AdminURLHelper(FilterableAdminURLHelper 似乎不是标准实现)。
否则,您将发生一些奇怪的重定向。
是否可以在顶部面板上添加一个额外的按钮(按钮),如图所示?
我在 Google 和这里没有找到任何东西。
屏幕截图上不清楚您是使用 modeladmin
还是 snippet
向用户公开此模型,但我假设是前者。
我不知道允许您直接将按钮添加到 header 的挂钩,但是模板 uses blocks 应该允许我们只覆盖这一部分。
我们可以利用模板的 resolution order 并创建优先于 /modeladmin/index.html
的 templates/modeladmin/app-name/model-name/index.html
。因此,如果您的应用程序名为 feedler
,模型名为 Entry
,请创建具有以下内容的 /modeladmin/feedler/entry/index.html
:
{% extends "modeladmin/index.html" %}
{% block header_extra %}
<a href="#">My New Button</a>
{{ block.super }}{% comment %}Display the original buttons {% endcomment %}
{% endblock %}
目前您的按钮没有太多作用。要创建将与该模型管理员交互的操作,您需要创建一些 button/url/permission helpers 和一个视图。
假设该操作正在将 objects 导出到 CSV 文件。振作起来,前面有很多代码。
在 /feedler/admin.py
中,创建 button/url/permission 助手并查看:
from django.contrib.auth.decorators import login_required
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.functional import cached_property
from django.utils.translation import ugettext as _
from wagtail.contrib.modeladmin.helpers import AdminURLHelper, ButtonHelper
from wagtail.contrib.modeladmin.views import IndexView
class ExportButtonHelper(ButtonHelper):
"""
This helper constructs all the necessary attributes to create a button.
There is a lot of boilerplate just for the classnames to be right :(
"""
export_button_classnames = ['icon', 'icon-download']
def export_button(self, classnames_add=None, classnames_exclude=None):
if classnames_add is None:
classnames_add = []
if classnames_exclude is None:
classnames_exclude = []
classnames = self.export_button_classnames + classnames_add
cn = self.finalise_classname(classnames, classnames_exclude)
text = _('Export {}'.format(self.verbose_name_plural.title()))
return {
'url': self.url_helper.get_action_url('export', query_params=self.request.GET),
'label': text,
'classname': cn,
'title': text,
}
class ExportAdminURLHelper(AdminURLHelper):
"""
This helper constructs the different urls.
This is mostly just to overwrite the default behaviour
which consider any action other than 'create', 'choose_parent' and 'index'
as `object specific` and will try to add the object PK to the url
which is not what we want for the `export` option.
In addition, it appends the filters to the action.
"""
non_object_specific_actions = ('create', 'choose_parent', 'index', 'export')
def get_action_url(self, action, *args, **kwargs):
query_params = kwargs.pop('query_params', None)
url_name = self.get_action_url_name(action)
if action in self.non_object_specific_actions:
url = reverse(url_name)
else:
url = reverse(url_name, args=args, kwargs=kwargs)
if query_params:
url += '?{params}'.format(params=query_params.urlencode())
return url
def get_action_url_pattern(self, action):
if action in self.non_object_specific_actions:
return self._get_action_url_pattern(action)
return self._get_object_specific_action_url_pattern(action)
class ExportView(IndexView):
"""
A Class Based View which will generate
"""
def export_csv(self):
data = self.queryset.all()
response = ...
return response
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
super().dispatch(request, *args, **kwargs)
return self.export_csv()
class ExportModelAdminMixin(object):
"""
A mixin to add to your model admin which hooks the different helpers, the view
and register the new urls.
"""
button_helper_class = ExportButtonHelper
url_helper_class = ExportAdminURLHelper
export_view_class = ExportView
def get_admin_urls_for_registration(self):
urls = super().get_admin_urls_for_registration()
urls += (
url(
self.url_helper.get_action_url_pattern('export'),
self.export_view,
name=self.url_helper.get_action_url_name('export')
),
)
return urls
def export_view(self, request):
kwargs = {'model_admin': self}
view_class = self.export_view_class
return view_class.as_view(**kwargs)(request)
在 /feedler/wagtail_hooks.py
中,创建并 register
ModelAdmin
:
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from .admin import ExportModelAdminMixin
from .models import Entry
class EntryModelAdmin(ExportModelAdminMixin, ModelAdmin):
model = Entry
# ...
modeladmin_register(EntryModelAdmin)
通过所有这些设置,您应该能够在上面创建的模板中使用 {% include 'modeladmin/includes/button.html' with button=view.button_helper.export_button %}
。
注意,如果您要列出页面,则需要从 PageAdminURLHelper 继承子类,而不是 AdminURLHelper(FilterableAdminURLHelper 似乎不是标准实现)。
否则,您将发生一些奇怪的重定向。