django - Adding a button to Wagtail Dashboard -


is possible add additional button(buttons) on top panel shown in picture?

enter image description here

i did't find in google , here.

it not clear on screenshot whether use modeladmin or snippets expose model user i'll assume former.

i don't know hook allows add buttons directly header, template uses blocks should allow overwrite part.

we can take advantage of resolution order of templates , create /modeladmin/app-name/model-name/index.html take precedence on /modeladmin/index.html. given app called feedler , model called entry, create /modeladmin/feedler/entry/index.html following content:

{% extends "modeladmin/index.html %}  {% block header_extra %}     <a href="#">my new button</a>     {{ block.super }}{% comment %}display original buttons {% endcomment %} {% endblock %} 

right button doesn't much. create action interact model admin, you'll need create button/url/permission helpers , view.

let's action exporting objects csv file. brace yourself, there's quite bit of code ahead.

in /feedler/admin.py, create button/url/permission helpers , view:

from django.contrib.auth.decorators import login_required django.core.urlresolvers import reverse django.utils.decorators import method_decorator django.utils.functional import cached_property django.utils.translation import ugettext _ wagtail.contrib.modeladmin.helpers import adminurlhelper, buttonhelper wagtail.contrib.modeladmin.views import indexview   class exportbuttonhelper(buttonhelper):     """     helper constructs necessary attributes create button.      there lot of boilerplate classnames right :(     """      export_button_classnames = ['icon', 'icon-download']      def export_button(self, classnames_add=none, classnames_exclude=none):         if classnames_add none:             classnames_add = []         if classnames_exclude 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):     """     helper constructs different urls.      overwrite default behaviour     consider action other 'create', 'choose_parent' , 'index'     `object specific` , try add object pk url     not want `export` option.      in addition, appends filters 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):     """     class based view 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):     """     mixin add model admin hooks different helpers, view     , register 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) 

in /feedler/wagtail_hooks.py, create , register modeladmin:

from wagtail.contrib.modeladmin.options import modeladmin, modeladmin_register  .admin import exportmodeladminmixin .models import entry  class entrymodeladmin(exportmodeladminmixin, modeladmin):     model = entry     # ...  modeladmin_register(entrymodeladmin) 

with setup, should able use {% include 'modeladmin/includes/button.html' button=view.button_helper.export_button %} in template created above.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -