2017-09-13 6 views

答えて

3

modeladminまたはsnippetを使用してこのモデルをユーザーに公開するかどうかは、スクリーンショットではわかりませんが、前者を仮定します。

ヘッダーに直接ボタンを追加できるフックについてはわかりませんが、この部分だけを上書きするテンプレート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 /許可ヘルパーおよびビューを作成します。/feedler/wagtail_hooks.py

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) 

、作成しregisterModelAdmin

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 %}を使用してください。

+0

ありがとうございます。私はあなたの指示に従ってすべてをやったが、残念ながらボタンは現れなかった。私はどこにでも間違いがないことを願っています。あなたのコードでは、この行が見つかりませんでした: 'from django.utils.decorators import method_decorator'。 ここに私の**モデルです** ** [github repo](https://github.com/khashashin/public-health-ch/blob/fd2ccf68eb488365a97d6fc49d0e1c8d6f4a5b49/feedler/models/models.py#L57)**テンプレート:* * [テンプレート](https://github.com/khashashin/public-health-ch/blob/fd2ccf68eb488365a97d6fc49d0e1c8d6f4a5b49/modeladmin/feedler/entry/index.html#L1) – khashashin

+0

不足しているインポートを指摘してくれてありがとうございます。私は答えを更新しました。現在表示されているボタンは、新しい[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'から継承させます。 –

+0

さらに、すべてのモデル管理ロジックを 'wagtail_hooks.py'または' admin.py'に移すことができます。答えを更新して階層を表示し(そしてモデル名を更新する) –

関連する問題