问题描述:
在xadmin框架中设置了bootswatch为True但是在xadmin的页面中主题仍然只有默认和BootStrap2,如下图
原因分析:
/xadmin/plugins/themes.py下的block_top_navmenu的问题,
当use_bootswatch 为True的时候,就会使用httplib2去访问http://bootswatch.com/api/3.json但是代开后会被替换为https,由于安全机制访问就会报错
解决方法:
使用cmd进入虚拟环境pip install requests,安装完成之后
在pycharm中到路径/xadmin/plugins/themes.py下
先import requests
再修改以下代码()
‘’‘
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()
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)
’‘’
回到原项目stop,再次run,没有报错,刷新浏览器中xadmin页面,检查一下
已解决