V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
honmaple
V2EX  ›  Python

flask 蓝图怎么加载配置文件

  •  
  •   honmaple · 2015-11-13 19:47:08 +08:00 · 3032 次点击
    这是一个创建于 3108 天前的主题,其中的信息可能已经有所发展或是发生改变。
    app/
    |----__init__.py
    |----templates/
    |----static/
    |----views/
    | |----__init__.py
    | |----admin.py
    config.py

    我想要在 admin.py 中加载配置文件
    总报 AttributeError: 'Blueprint' object has no attribute 'config'

    # app/__init__.py
    from flask import Flask
    from .views import admin

    app = Flask(__name__)
    app.register_blueprint(admin.site)
    app.config.from_object('config')

    # app/views/admin.py
    from flask import request, session, g, redirect, url_for, \
    render_template, flash, Blueprint
    from flask_flatpages import FlatPages

    site = Blueprint('admin', __name__)
    flatpages = FlatPages(site) #就是这行
    @site.route('/admin')
    def hello():
    pages = (p for p in flatpages if 'Date' in p.meta)
    return render_template("admin.html",pages=pages)
    4 条回复    2015-11-13 21:02:04 +08:00
    leeyiw
        1
    leeyiw  
       2015-11-13 20:14:41 +08:00
    from Flask import current_app as app

    然后用就可以了
    honmaple
        2
    honmaple  
    OP
       2015-11-13 20:21:58 +08:00
    @leeyiw 能否具体点
    leeyiw
        3
    leeyiw  
       2015-11-13 20:32:19 +08:00
    @honmaple 在蓝图代码里想访问 config ,使用 app.config 变量
    zhuangzhuang1988
        4
    zhuangzhuang1988  
       2015-11-13 21:02:04 +08:00
    current_app.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1318 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:58 · PVG 07:58 · LAX 16:58 · JFK 19:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.