I have some troubles about flask Blueprint
Structure of my project:
hw
...run.py
...sigcontoj
......__init__.py
......admin
.........__init__.py
.........views.py
.........models.py
......frontend
.........__init__.py
.........views.py
.........models.py
run.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"
The output of app.url_map is
Map([' (HEAD, OPTIONS, GET) -> static>])
And the index page is 404.
Are there any mistake in my code?
The issue you have is that even though you import the frontend Blueprint since you never import views the index (/) route is never registered with frontend. If you update sigcontoj/__init__.py to import sigcontoj.frontend.views:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sigcontoj.frontend import frontend
import sigcontoj.frontend.views
then everything should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With