zoukankan      html  css  js  c++  java
  • Django项目配置xadmin 无法加载主题切换功能

    开启主题切换功能,只需做如下设置,此种设置仅有两种默认的bootstrap主题:
    adminx.py
    class BaseSetting(object):
    # 添加主题功能
    enable_themes = True
    #使用更多主题
    use_bootswatch = True
    解决办法:
    阅读xadmin/plugins/themes.py源码进行错误排查,基本锁定错误发生在第75行。在访问bootswatch资源时超时。每次服务重启,第一次进入系统都要等1~2分钟也是因为这里的问题

    浏览器访问https://bootswatch.com/api/3.json查看所有可用主题。将可用主题的name,css参数记录到user_themes参数中即可。

    修改为:
    class BaseSetting(object):
    # 添加主题功能
    enable_themes = True
    use_bootswatch = True
    user_themes = [{'name': 'Cerulean', 'css': 'https://bootswatch.com/3/cerulean/bootstrap.css'},
    {'name': 'Cosmo', 'css': 'https://bootswatch.com/3/cosmo/bootstrap.css'},
    {'name': 'Cyborg', 'css': 'https://bootswatch.com/3/cyborg/bootstrap.css'},
    {'name': 'Darkly', 'css': 'https://bootswatch.com/3/darkly/bootstrap.css'},
    {'name': 'Flatly', 'css': 'https://bootswatch.com/3/flatly/bootstrap.css'},
    {'name': 'Journal', 'css': 'https://bootswatch.com/3/journal/bootstrap.css'},
    {'name': 'Lumen', 'css': 'https://bootswatch.com/3/lumen/bootstrap.css'},
    {'name': 'Paper', 'css': 'https://bootswatch.com/3/paper/bootstrap.css'},
    {'name': 'Readable', 'css': 'https://bootswatch.com/3/readable/bootstrap.css'},
    {'name': 'Sandstone', 'css': 'https://bootswatch.com/3/sandstone/bootstrap.css'},
    {'name': 'Simplex', 'css': 'https://bootswatch.com/3/simplex/bootstrap.css'},
    {'name': 'Slate', 'css': 'https://bootswatch.com/3/slate/bootstrap.css'},
    {'name': "Superhero", 'css': "https://bootswatch.com/3/superhero/bootstrap.css"},
    {'name': "United", 'css': "https://bootswatch.com/3/united/bootstrap.css"},
    {'name': "Yeti", 'css': "https://bootswatch.com/3/yeti/bootstrap.css"},
    ]
    themes.py中
    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:
                    headers = {"Accept": "application/json", "User-Agent": self.request.META['HTTP_USER_AGENT']}
                    content = requests.get("https://bootswatch.com/api/3.json", headers=headers)
                    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)
    

    修改为:
    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("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']
                        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)
  • 相关阅读:
    TP5.0防跳墙访问
    TP5.0上传添加数据库
    抽象类与接口
    instanceof 关键字
    abstract 抽象类
    extends 继承
    单例模式
    类的加载过程和对象的创建
    静态成员变量和非静态成员变量的区别
    this关键字
  • 原文地址:https://www.cnblogs.com/superroshan/p/13273345.html
Copyright © 2011-2022 走看看