前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Django+SimpleUI技术分享

Django+SimpleUI技术分享

作者头像
小颜同学
发布2024-05-29 12:02:19
1160
发布2024-05-29 12:02:19
举报
文章被收录于专栏:原创笔记原创笔记

一、Django可视化后台自定义菜单。

代码语言:javascript
复制
SIMPLEUI_CONFIG = {
    'system_keep': False,  # 关闭系统菜单
    'menu_display': ['首页大屏','认证和授权','宏观质量', '品牌建设', '产品安全', '企业质量画像',
                     '企业基础设施'],
    'dynamic': False,  # 设置是否开启动态菜单, 默认为False. 如果开启, 则会在每次用户登陆时动态展示菜单内容
    'menus': [{
        'name': 'Simpleui',
        'icon': 'fas fa-code',
        'url': 'https://gitee.com/tompeppa/simpleui',
        'codename': 'simpleui'
    }, {
        'name': '在线社区',
        'icon': 'fa fa-file',
        'codename': 'test',
        'models': [{
            'name': 'SimplePro',
            'icon': 'far fa-surprise',
            'models': [{
                'name': 'Pro文档',
                'url': 'https://simpleui.72wo.com/docs/simplepro'
            }, {
                'name': '购买Pro',
                'url': 'http://simpleui.72wo.com/simplepro'
            }]
        }, {
            'name': '社区',
            'url': 'https://simpleui.72wo.com',
            'icon': 'fab fa-github'
        }, {
            'name': '图片转换器',
            'url': 'https://convert.72wo.com',
            'icon': 'fab fa-github',
            'codename': 'nat'
        }]
    }]
}

注意:此代码块在setting.py中使用,如果需要搭配使用,需要models模型在admin.py中进行注册之后即可映射出url替代原文中的url。此自定义菜单支持多级菜单。django后台会直接以APP名称为一级菜单,数据表为二级菜单,最多到二级菜单,没法到三级菜单。我们安装simpleui可以解决这个。 另外需要注意的是,一级菜单必须在menu_display里命名,并且两者名字必须一致,不然可能会出现菜单列表丢失的现象。

二、Django中admin.py的花式操作

添加自定义导出表头按钮:

Admin

代码语言:javascript
复制
class TestAdmin(ImportExportModelAdmin):
    list_display = ['year', 'std_type', 'std_num']
    search_fields = ['std_num']
    list_per_page = 20
	# 导出表头方法
    def export_table_header(self, request, queryset):
        resource = TestResource()
        headers = resource.get_export_headers()

        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="table_header.csv"'

        writer = csv.writer(response)
        writer.writerow(headers)

        return response
	# 自定义表头按钮名称
    export_table_header.short_description = '导出表头'
    # 添加自定义动作
    actions = ['export_table_header']
    
    resource_class = TestResource

Resource

代码语言:javascript
复制
class TestResource(resources.ModelResource):
     class Meta:
        model = Test
        fields = ('id', 'year', 'std_type', 'std_num', 'dt')  # 自定义指定需要导出的字段
        export_order = ('year', 'std_type', 'std_num', 'dt')
        
            def export_headers_to_csv(self):
        dataset = self.export(queryset)
        response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
        response['Content-Disposition'] = 'attachment; filename="exported_data.xlsx"'
        dataset.export(response)
        return response

    def get_export_headers(self):
        headers = []
        for field_name, field in self.fields.items():
            chinese_name = self.field_mapping.get(field.attribute, field.column_name)
            headers.append(chinese_name)
        return headers
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、Django可视化后台自定义菜单。
  • 二、Django中admin.py的花式操作
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档