推荐学习书目
› 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 · Nov 13, 2015 · 3839 views
    This topic created in 4013 days ago, the information mentioned may be changed or developed.
    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 replies  •  2015-11-13 21:02:04 +08:00
    leeyiw
    leeyiw
    Nov 13, 2015
    1
    from Flask import current_app as app

    然后用就可以了
    honmaple
    honmaple
    OP
    Nov 13, 2015
    2
    @leeyiw 能否具体点
    leeyiw
    leeyiw
    Nov 13, 2015
    3
    @honmaple 在蓝图代码里想访问 config ,使用 app.config 变量
    zhuangzhuang1988
    zhuangzhuang1988
    Nov 13, 2015
    4
    current_app.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1642 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:14 · PVG 21:14 · LAX 05:14 · JFK 08:14
    ♥ Do have faith in what you're doing.