您是否使用modeladmin
或snippet
s揭露这种模式给用户,但我会认为前者是不上截图清楚。
我不知道一个钩子,它允许你直接添加按钮头,但模板uses blocks应该允许我们覆盖只是这一部分。
我们可以采取的模板resolution order的优势,创造/modeladmin/app-name/model-name/index.html
将优先于/modeladmin/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 %}
现在您的按钮并没有做太多。要创建一个行动,这将与模型管理器交互,你需要创建一些按钮/ URL /许可helpers和视图。
比方说,行动是出口对象到CSV文件。打起精神来,有相当多的代码领先。
在/feedler/admin.py
,创建按钮/ URL /许可佣工和看法:
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers 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(FilterableAdminURLHelper):
"""
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似乎并没有成为一个标准执行)。 否则,你将有一些奇怪的重定向发生。