烧瓶项目结构 [英] Flask project structure

查看:135
本文介绍了烧瓶项目结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用Flask时要遵循的最佳文件夹结构。我想达到以下目的:

$ my $ b $ runserver.py
$ app
$ b $ app2
....



当然,我想与我的所有应用程序共享我的数据库配置。我怎样才能做到这一点?在文档中,他们总是谈论一个应用程序



PD:我从django来的。
PD2:我也读过: http://flask.pocoo.org/docs / blueprints / 和这个: http:// flask。 pocoo.org/docs/patterns/packages/#modules-and-resources

解决方案

我发现自己最好对我来说就是把应用程序分成蓝图。也就是说,将整个事件分解成单独的WSGI应用程序,而不是分开注册到Flask应用程序中的Flask类对象。它们提供了注册错误处理程序,模板上下文处理器等的可能性,以便注册为蓝图端点的视图或整个应用程序 - 您的选择。

共享数据库连接对象可以通过使用名为request_globals_class的类来完成(它必须在你的应用程序类中声明,当然这继承了Flask)。当你为这个类提供一个属性时,它就可以作为一个视图(或者在请求处理上下文中运行的任何一个)作为flask.g对象的一个​​属性。


I want to know what is the best folder structure to follow when using Flask. I want to achieve the following:

/myproject runserver.py /app1 ... /app2 ....

And of course I want to share my database configuration with all my apps. How can I achieve this? In the documentation they always talk of ONE app

PD: I'm comming from django. PD2: I've also read this: http://flask.pocoo.org/docs/blueprints/ and this: http://flask.pocoo.org/docs/patterns/packages/#modules-and-resources

解决方案

I found myself that best for me is to divide an application into blueprints. That is, split the whole thing not into separate WSGI applications but rather into these Flask-like objects which get registered in the Flask app. They provide possibilities to register errorhandlers, template context processors, etc. for views registered as endpoints of blueprint or for whole application - your choice.

Sharing of database connection object can be done through use of class with name "request_globals_class" (it must be declared in your application class which of course inherits Flask). When you provide an attribute for this class it is then accessible for a view (or whatever runs in request handling context) as an attribute of flask.g object.

这篇关于烧瓶项目结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆