1
A
回答
3
截图中不清楚您使用的是modeladmin
还是snippet
s将此模型公开给用户,但我会假设前者。
我不知道一个钩子,它允许你直接将按钮添加到标题,但是模板uses blocks应该允许我们覆盖这个部分。
我们可以利用模板的resolution order并创建/modeladmin/app-name/model-name/index.html
,这将优先于/modeladmin/index.html
。
{% extends "modeladmin/index.html %}
{% block header_extra %}
<a href="#">My New Button</a>
{{ block.super }}{% comment %}Display the original buttons {% endcomment %}
{% endblock %}
现在您的按钮并没有做太多:所以您的应用程序称为feedler
和一个叫Entry
模型,创建/modeladmin/feedler/entry/index.html
具有以下内容给出。要创建一个将与该模型管理员交互的动作,您需要创建一些按钮/网址/权限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 %}
。
相关问题
- 1. 奏鸣曲管理员:如何从仪表板删除“添加新的”按钮?
- 2. 向UIPickerView添加按钮
- 3. 向Joomla管理仪表板添加一个“大图标”
- 4. Mobikwik仪表板仪表板
- 5. 复制到剪贴板仪表板按钮
- 6. 如何为自动更新仪表板添加Deepsee仪表板小部件?
- 7. Cortana仪表板不加载
- 8. 添加的TextBlock按钮模板
- 9. 与按钮添加面板中点击
- 10. 将按钮动态添加到面板
- 11. 当一个按钮被移除时,带有按钮的CSS仪表板崩溃
- 12. 如何在仪表板模板中添加禁止的警报?
- 13. admob仪表板
- 14. 将按钮从包装面板添加到按钮阵列
- 15. 向gwt添加更多按钮Suggest Box
- 16. 使用ActionBarSherlock向ActionBar添加按钮
- 17. swift:向一组按钮添加动作
- 18. 向按钮添加事件监听器
- 19. 向单选按钮添加标签
- 20. 向UITableViewCell添加一个按钮
- 21. WPF:向按钮添加触发器
- 22. 向jQuery添加刷新按钮Datatable
- 23. 向按钮添加自动布局
- 24. 向UINavigationController添加标题+按钮
- 25. 向后退按钮添加操作
- 26. 向单元格添加按钮
- 27. 向按钮添加/删除类
- 28. 如何向Eclipse IDE添加新按钮
- 29. 向ABPersonViewController添加额外的按钮
- 30. 添加按钮
非常感谢您的解释。我根据你的指示做了一切事情,但不幸的是这个按钮没有出现。我希望我没有错误的地方。在您的代码中,缺少此行: 'from django.utils.decorators import method_decorator'。 这是我的**模型:** [github回购](https://github.com/khashashin/public-health-ch/blob/fd2ccf68eb488365a97d6fc49d0e1c8d6f4a5b49/feedler/models/models.py#L57)** template:* * [template](https://github.com/khashashin/public-health-ch/blob/fd2ccf68eb488365a97d6fc49d0e1c8d6f4a5b49/modeladmin/feedler/entry/index.html#L1) – khashashin
感谢您指出缺少的导入。我已经更新了答案。至于按钮现在显示,我认为这是因为你没有注册新的[ModelAdmin](https://github.com/khashashin/public-health-ch/blob/fd2ccf68eb488365a97d6fc49d0e1c8d6f4a5b49/feedler/models/models.py#L166 ),但仍然注册[旧的](https://github.com/khashashin/public-health-ch/blob/fd2ccf68eb488365a97d6fc49d0e1c8d6f4a5b49/feedler/wagtail_hooks.py#L8)。更新'wagtail_hooks.py'中的'EntryModelAdmin'并使其从'ExportModelAdminMixin'继承。 –
此外,您可以将所有模型管理逻辑移动到'wagtail_hooks.py'或'admin.py'。我将更新答案以显示层次结构(并更新为您的型号名称)。 –