当使用下面方式引入xadmin样式的时候发现并没有效果:
file:adminx.py
xadminxadmin views BaseSetting(): enable_themes = use_bootswatch = Some Codes...xadmin.site.register(views.CommAdminViewGlobalSettings)
file:__init__.py
default_app_config = "users.apps.UsersConfig"
错误原因:
当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.
在xadmin的源码目录下修改xadmin\plugins\themes.py:
#coding:utf-8from __future__ import print_functionimport httplib2from django.template import loaderfrom django.core.cache import cachefrom django.utils import sixfrom django.utils.translation import ugettext as _from xadmin.sites import sitefrom xadmin.models import UserSettingsfrom xadmin.views import BaseAdminPlugin, BaseAdminViewfrom xadmin.util import static, jsonimport sixif six.PY2: import urllibelse: import urllib.parseimport requestsTHEME_CACHE_KEY = 'xadmin_themes' class ThemePlugin(BaseAdminPlugin): enable_themes = False # {'name': 'Blank Theme', 'description': '...', 'css': 'http://...', 'thumbnail': '...'} user_themes = None use_bootswatch = False default_theme = static('xadmin/css/themes/bootstrap-xadmin.css') bootstrap2_theme = static('xadmin/css/themes/bootstrap-theme.css') def init_request(self, *args, **kwargs): return self.enable_themes def _get_theme(self): if self.user: try: return UserSettings.objects.get(user=self.user, key="site-theme").value except Exception: pass if '_theme' in self.request.COOKIES: if six.PY2: func = urllib.unquote else: func = urllib.parse.unquote return func(self.request.COOKIES['_theme']) return self.default_theme def get_context(self, context): context['site_theme'] = self._get_theme() return context # Media def get_media(self, media): return media + self.vendor('jquery-ui-effect.js', 'xadmin.plugin.themes.js') # Block Views 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: # h = httplib2.Http() # resp, content = h.request("https://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'] # ex_themes.extend([ # {'name': t['name'], 'description': t['description'], # 'css': t['cssMin'], 'thumbnail': t['thumbnail']} # for t in watch_themes]) 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})) site.register_plugin(ThemePlugin, BaseAdminView)
然后就可以调出企业常用的各种主题样式了。
原文地址:http://blog.51cto.com/xvjunjie/2084672
时间: 2024-10-10 22:27:11