【问题标题】:flask Blueprint file structure烧瓶蓝图文件结构
【发布时间】:2013-04-19 07:14:47
【问题描述】:

我对烧瓶蓝图有些烦恼

我的项目结构:

hw
...run.py
...sigcontoj
......__init__.py
......admin
.........__init__.py
.........views.py
.........models.py
......frontend
.........__init__.py
.........views.py
.........models.py

运行.py:

from sigcontoj import create_app
from sigcontoj.frontend import frontend


app = create_app(__name__)


if __name__ == '__main__':
    print app.url_map
    print app.blueprints
    app.run(debug = True)

sigcontoj__init__.py:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sigcontoj.frontend import frontend


db = SQLAlchemy()

def create_app(name=__name__):
    app = Flask(name, static_path='/static')
    app.register_blueprint(frontend, url_prefix=None)
    app.secret_key = 'dfsdf1323jlsdjfl'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///soj.db'
    db.init_app(app)
    return app

sigcontoj\frontend__init__.py:

from flask import Blueprint

frontend = Blueprint('frontend', __name__, template_folder='templates')

sigcontoj\frontend\models.py:

from datetime import datetime
from sigcontoj import db


class News(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(256))
    content = db.Column(db.Text)
    publish_time = db.Column(db.DateTime, default=datetime.now())

    def __repr__(self):
        return '<News : %s>' % self.title

sigcontoj\frontend\views.py:

from sigcontoj.frontend.models import News
from sigcontoj.frontend import frontend


@frontend.route('/')
def index():
    news = News.query.all()[0:5]
    return "hello world"

app.url_map 的输出是

Map([' (HEAD, OPTIONS, GET) -> 静态>])

而且索引页是404。

我的代码有错误吗?

【问题讨论】:

    标签: python flask blueprint


    【解决方案1】:

    您遇到的问题是,即使您导入 frontend 蓝图,因为您从未导入 viewsindex (/) 路由也从未向 frontend 注册。如果您更新sigcontoj/__init__.py 以导入sigcontoj.frontend.views

    from flask import Flask
    from flask.ext.sqlalchemy import SQLAlchemy
    from sigcontoj.frontend import frontend
    import sigcontoj.frontend.views
    

    那么一切都会正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 2012-06-16
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      相关资源
      最近更新 更多