class Blog(object):
def init(self, app, db, url_prefix):
self.app = app
self.db = db
self.init_models()
self.init_blueprint()
def init_routes(self):
self.blueprint = Blueprint('blog', __name__)
self.app.register_blueprint(self.blueprint, url_prefix=url_prefix)
self.blueprint.add_url_rule('/post/<int:post_id>', self.get_post)
... объявляем здесь другие роуты ...
def get_post(self, post_id):
return render_template('post.html', post=self.Post.filter(id=post_id).first())
class Blog(object):
def init(self, app, db):
self.app = app
self.db = db
self.init_models()
def init_models(self):
# классы в python можно объявить внутри функции
class Post(self.db.Model):
...blah blah blah...
self.Post = Post
app = Flask('foobar', __name__)
db = SQLAlchemy()
blog = Blog(app, db)
post = blog.Post(title='...', article='...')
db.session.save(post)