xadmin下设置“use_bootswatch = True”无效的原因

来源:互联网 发布:科尔沃折叠刀淘宝 编辑:程序博客网 时间:2024/06/05 05:51

最近一段时间在学习django,使用xadmin做为后台管理系统,在设置“use_bootswatch = True”时无效,只有默认的一种本地式样,好吧有事找百度,从https://my.oschina.net/u/2396236/blog/1083251博客中得到解决方法,按照博客修改/xadmin/plugins/themes.py,运行又出现expected string or buffer python错误,应该是json解析数据出现的格式错误,增加

content = json.dumps(content.json())

处理json数据,成功解决问题。

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

在xadmin使用的过程中,设置“use_bootswatch = True”,企图调出主题菜单,显示更多主题。然而设置了后,发现主题还是默认和bootstrap2,深入跟踪源代码,发现/xadmin/plugins/themes.py下的

block_top_navmenu

方法,当use_bootswatch 为True的时候,就会使用httplib2去

http://bootswatch.com/api/3.json

网址获取主题菜单项。但是使用浏览器打开这个网址,http会被替换成https的。httplib2访问这个https的网址,就会报错。报错信息为:

[SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure 

但是这个报错信息,不知道为嘛,没显示在后台...

httplib2访问这个网址,我没试验成功,于是我使用requests库来替代httplib2.
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#假如为True使用原来的代码,假如为Flase,使用requests库来访问                    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']})                        if six.PY3:                            content = content.decode()                        watch_themes = json.loads(content)['themes']                    else:                        content = requests.get("https://bootswatch.com/api/3.json")                        if six.PY3:                            content = content.text.decode()content = json.dumps(content.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}))

阅读全文
0 0
原创粉丝点击