1

在学习教程时,@ask 装饰器在与 ask = Ask(app, "/someroute") 和 app = Flask( name ) 位于同一文件中时起作用。如果我希望将@ask 装饰器放在其自己文件夹中的视图文件中,无论我尝试什么都使用蓝图,我会得到装饰器的“名称'ask'未定义”

我应该说我是烧瓶的新手,但现在有一个基本应用程序可以使用诸如烧瓶主体与烧瓶登录和烧瓶导航之类的东西,只有在允许的情况下才能显示菜单项。

我一直在关注许多烧瓶询问教程并且一切正常(到目前为止),但我更愿意将视图代码放在单独的views.py中,并在其自己的文件夹中使用蓝图。我使用 create_app(config_name) 而不是 app = Flask( name ) 正如许多烧瓶教程中所建议的那样。我希望这样做,因为我想保持我的主要 __init__.py 尽可能干净。

我对烧瓶的新认识使我无法按照 Gitter 上的某些人的建议在烧瓶中使用 alexa-skills-kit-sdk-for-python 之类的事情(因为似乎不再维护烧瓶询问),我仍然不' 对烧瓶的理解不足以弄清楚为什么在单独的views.py 中没有询问对象

实际上,我很想将所有内容都放在 app/__init__.py 中,因为这只是一个爱好应用程序,但我正在尝试按照许多博客的建议来构建我的烧瓶应用程序,即如果没有明显的解决方法,那么我将采用可行的方式,但我不禁觉得这是我从烧瓶的角度做错的事情,而不是烧瓶问。

可能值得补充的是,我遇到了一些安装问题(在 PyYAML 上窒息,所以我下载了软件包并手动安装了要求,然后运行 ​​setup.py(以防万一对问题产生影响)

#app/__init__.py
#Pytho 3

from flask_ask import Ask, statement, question, session

....

def create_app(config_name):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_object(app_config[config_name])
    app.config.from_pyfile('config.py')

    Bootstrap(app)
    db.init_app(app)
    nav.init_app(app)
    toolbar.init_app(app)

    login_manager.init_app(app)
    login_manager.login_message = "You must be logged in to access this page."
    login_manager.login_view = "auth.login"    

    principals = Principal(app)

    ask = Ask(app, "/alexa")
    ask.init_app(app) #have also done it without this as some of the tutorials don't mention it

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)

    from .home import home as home_blueprint
    app.register_blueprint(home_blueprint)

    from .alexa import alexa as alexa_blueprint
    app.register_blueprint(alexa_blueprint,url_prefix='/alexa')



    # This loads fine 
    @ask.launch
    def start_skill():
        welcome_message = 'Hello there, would you like the news?'
        return question(welcome_message)

如果我删除 @ask 装饰器代码并将其放在下面......

#app/alexa/views.py
from flask_ask import Ask, statement, question, session
import json
import requests
import time
import unidecode

from . import alexa

    @alexa.route('/')
    def alexa_route():
     return('alexa')

    #this doesn't work
    @ask.launch # this is on line 33 (as below error)
    def start_skill():
        welcome_message = 'Hello there, would you like the news?'
        return question(welcome_message)

我明白了(我正在使用 venv 所以路径是虚拟环境)

文件“/Users/username/Documents/Personal/Pi/template/lib/python3.7/site-packages/flask/app.py”,第 2309 行,调用中 返回 self.wsgi_app(environ, start_response) 文件“/Users/username/Documents/Personal/Pi/template/lib/python3.7/site-packages/flask/app.py”,第 2295 行,在 wsgi_app response = self. handle_exception(e) 文件“/Users/username/Documents/Personal/Pi/template/lib/python3.7/site-packages/flask/app.py”,第 1741 行,在 handle_exception reraise(exc_type, exc_value, tb) 文件中“/Users/username/Documents/Personal/Pi/template/lib/python3.7/site-packages/flask/_compat.py”,第 35 行,在 reraise raise value File “/Users/username/Documents/Personal/Pi /template/lib/python3.7/site-packages/flask/app.py”,第 2292 行,在 wsgi_app 响应 = self.full_dispatch_request() 文件“/Users/username/Documents/Personal/Pi/template/lib/python3 .7/site-packages/flask/app.py”,第 1815 行,在 full_dispatch_request rv = self.handle_user_exception(e) 文件“/Users/username/Documents/Personal/Pi/template/lib/python3.7/site-packages/flask/app.py”,第 1718 行,在 handle_user_exception reraise(exc_type, exc_value, tb) 文件中“/Users/username/Documents/Personal/Pi/template/lib/python3.7/site-packages/flask/_compat.py”,第 35 行,在 reraise raise value File “/Users/username/Documents/Personal/Pi /template/lib/python3.7/site-packages/flask/app.py”,第 1813 行,在 full_dispatch_request rv = self.dispatch_request() 文件“/Users/username/Documents/Personal/Pi/template/lib/python3 .7/site-packages/flask_debugtoolbar//Users/username/Documents/Personal/Pi/template/lib/python3.7/site-packages/flask/_compat.py”,第 35 行,在 reraise raise value 文件“/Users/username/Documents/Personal/Pi/模板/lib/python3.7/site-packages/flask/app.py”,第 1813 行,在 full_dispatch_request rv = self.dispatch_request() 文件“/Users/username/Documents/Personal/Pi/template/lib/python3. 7/站点包/flask_debugtoolbar//Users/username/Documents/Personal/Pi/template/lib/python3.7/site-packages/flask/_compat.py”,第 35 行,在 reraise raise value 文件“/Users/username/Documents/Personal/Pi/模板/lib/python3.7/site-packages/flask/app.py”,第 1813 行,在 full_dispatch_request rv = self.dispatch_request() 文件“/Users/username/Documents/Personal/Pi/template/lib/python3. 7/站点包/flask_debugtoolbar/init .py",第 125 行,在 dispatch_request 返回 view_func(**req.view_args) 文件 "/Users/username/Documents/Personal/Pi/baseapp/app/alexa/views.py",第 33 行,在 alexa_route return(询问)NameError:未定义名称“询问”

4

1 回答 1

1

使用烧瓶的 app-factory 时,您应该在 create_app 之外创建模块的对象:

#my_app/__init__.py

from flask_ask import Ask, statement, question, session
...
ask = Ask()

def create_app(config_name):
    app = Flask(...)
    ...

    ask.init_app(app)

然后,您可以在需要时导入对象:

#my_app/alexa/views.py
from my_app import ask
...

@ask.launch
def start_skill():
    welcome_message = 'Hello there, would you like the news?'
    return question(welcome_message)

请注意将您的应用命名为“应用”,因为这可能会造成混淆。为了更清楚,我自由地将其更改为“my_app”。

于 2019-02-05T13:07:48.550 回答