解决xadmin下设置“use_bootswatch = True”无效的问题

环境:
python 2.7
django 1.9
xadmin采用源代码的方式引入到项目中QQ群交流:697028234

 

1.安装requests

pip install requests


2./xadmin/plugins/themes.py 引入requests

import requests


3.修改block_top_navmenu方法:

    def block_top_navmenu(self, context, nodes):

        themes = [
            {'name': _(u"Default"), 'description': _(u"Default bootstrap theme"), 'css': self.default_theme},
            {'name': _(u"Bootstrap2"), 'description': _(u"Bootstrap 2.x theme"), 'css': self.bootstrap2_theme},
        ]
        select_css = context.get('site_theme', self.default_theme)

        if self.user_themes:
            themes.extend(self.user_themes)

        if self.use_bootswatch:
            ex_themes = cache.get(THEME_CACHE_KEY)
            if ex_themes:
                themes.extend(json.loads(ex_themes))
            else:
                ex_themes = []
                try:
                    flag = False
                    if flag:
                        h = httplib2.Http()
                        resp, content = h.request("http://bootswatch.com/api/3.json", 'GET', '',
                                                  headers={"Accept": "application/json",
                                                           "User-Agent": self.request.META['HTTP_USER_AGENT']})
                        watch_themes = json.loads(content)['themes']
                    else:
                        content = requests.get("https://bootswatch.com/api/3.json")
                        watch_themes = json.loads(content.text)['themes']

                    ex_themes.extend([
                                         {'name': t['name'], 'description': t['description'],
                                          'css': t['cssMin'], 'thumbnail': t['thumbnail']}
                                         for t in watch_themes])
                except Exception as e:
                    print(e)

                cache.set(THEME_CACHE_KEY, json.dumps(ex_themes), 24 * 3600)
                themes.extend(ex_themes)

        nodes.append(
            loader.render_to_string('xadmin/blocks/comm.top.theme.html', {'themes': themes, 'select_css': select_css}))

参考网址:https://my.oschina.net/u/2396236/blog/1083251

posted @ 2017-09-08 11:44  巴布非  阅读(1027)  评论(0编辑  收藏  举报