問題描述:
在xadmin框架中設置了bootswatch為True但是在xadmin的頁面中主題仍然只有默認和BootStrap2,如下圖
只有默認和Bootsstrap2
原因分析:
/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頁面,檢查一下
主題都出現了
已解決