Python 如何在flask应用程序中包含来自其他模块的视图?
我是Flask的新手,我正在尝试找到扩展以下应用程序视图的最简单方法:Python 如何在flask应用程序中包含来自其他模块的视图?,python,flask,Python,Flask,我是Flask的新手,我正在尝试找到扩展以下应用程序视图的最简单方法: import os from flask import Flask def create_app(test_config=None): # create and configure the app app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY='de
import os
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# a simple page that says hello
@app.route('/hello')
def hello():
return 'Hello, World!'
return app
我读了一些蓝图上的文件,这似乎有点复杂。我的问题是:
从另一个文件向此创建应用程序功能添加视图的最简单方法是什么
如果是通过使用蓝图,那就这样吧,但这样的事情可能吗
###views.py
init.py
如果您不想使用蓝图,那么请记住装饰器只是函数调用
@app.route('/hello')
def hello():
return 'Hello, World!'
完全一样
def hello():
return 'Hello, World!'
hello = app.route('/hello')(hello)
有了这些知识,你就可以
from views import goodbye
# ...
app.route('/goodbye')(goodbye)
为避免这种奇怪的双重调用语法,请执行以下操作:
但是,您知道,最好阅读关于“大型应用程序”的文章:
def hello():
return 'Hello, World!'
hello = app.route('/hello')(hello)
from views import goodbye
# ...
app.route('/goodbye')(goodbye)
app.add_url_rule('/', 'goodbye', goodbye)