1. 程式人生 > >xadmin下設定“use_bootswatch = True”無效

xadmin下設定“use_bootswatch = True”無效

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

這邊使用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() 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