---
title: "xadmin列表页添加自定义工具栏toolbar"
author: "iRoyce"
date: "2017-02-06T23:54:25Z"
tags: []
categories: ["互联网"]
slug: "xadmin-toolbar"
draft: false
lastmod: "2017-02-06T23:58:41Z"
id: 2355813
url: "https://iroyce.iteye.com/blog/2355813"
---

通过xadmin的Plugin实现，adminx.py中

 

 

```
class Link2AdminPlugin(BaseAdminPlugin):
    link_2_admin = False

    def init_request(self, *args, **kwargs):
        return bool(self.link_2_admin)

    #my_top_toolbar为xadmin model_list.html中存在的view_block区块
    def block_my_top_toolbar(self, context, nodes):
       nodes.append(loader.render_to_string('product/blocks/my_top_toolbar.link_2_admin.html', context_instance=context))

xadmin.site.register_plugin(Link2AdminPlugin, ListAdminView)
```

 

 

可以简单改写xadmin model\_list.html中的代码，实现插入列表页任意地方，如model\_list.html中加入

 

```
<div class="btn-toolbar pull-right" role="toolbar" aria-label="...">
    {% view_block 'my_top_toolbar' %}&nbsp;&nbsp;
  </div>
```

 

 

block\_my\_top\_toolbar方法在nodes上增加一个form表单，实现一些数据的提交，admin类中的get或post方法接收表单提交并处理业务逻辑。