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

Posted 代码变革世界

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决xadmin下设置“use_bootswatch = True”无效的问题相关的知识,希望对你有一定的参考价值。

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

 

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

以上是关于解决xadmin下设置“use_bootswatch = True”无效的问题的主要内容,如果未能解决你的问题,请参考以下文章

xadmin下设置“use_bootswatch = True”无效解决方法

xadmin的主题use_bootswatch = True”无效解决方法

Django2+xadmin 设置 relfield_style='fk-ajax' 搜索框不起作用的解决方法

手动配置xadmin文件

Django3.0使用xadmin相关报错解决

xadmin为啥设置了use