简体   繁体   English

无法使用 Flask 应用程序工厂和 Flask-Restful 添加资源

[英]Unable to add resources using a Flask Application Factory and Flask-Restful

I'm writing in a bit of desperation.我写的有点绝望。 I have an application that uses Flask, Flask Restful, JWT, Flask SQLAlchemy. I have an application that uses Flask, Flask Restful, JWT, Flask SQLAlchemy.

It's a personal project which is quite large and is suffering from lack of unit testing - so I've been adding unit testing using pytest and tox.这是一个相当大的个人项目,并且缺乏单元测试 - 所以我一直在使用 pytest 和 tox 添加单元测试。

I realized that to do this properly, I needed to move from the我意识到要正确地做到这一点,我需要从

from flask import Flask
app = Flask(__name__)

to using an application factory so I can setup different environments easily.使用应用程序工厂,以便我可以轻松设置不同的环境。 I am completely stuck and feeling more than a little stupid.我完全被困住了,感觉有点愚蠢。 To get help and show the issue, I have stripped things down to this:为了获得帮助并显示问题,我将事情简化为:

The 'old' way: “旧”方式:

resources/talk.py:

from flask_restful import Resource

class Talk(Resource):
    def get(self):
        return { 'msg' : 'Talking' }

app.py:

from flask import Flask
from flask_restful import Api
from resources.talk import Talk

app = Flask(__name__)
api = Api(app)

api.add_resource(Talk, '/talk')

if __name__ == "__main__":
    app.run(debug=True)

If I run this and go to http://localhost:5000/talk I get the { 'msg': 'Talking' } response.如果我运行这个和 go 到 http://localhost:5000/talk 我得到 { 'msg': 'Talking' } 响应。

However, I cannot get it to work using an application factory.但是,我无法使用应用程序工厂使其工作。

I have looked at the docs from Flask, which are here: https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/#我查看了 Flask 的文档,这些文档位于: https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/#

And it makes sense.这是有道理的。 However, I can't get the url to work.但是,我无法让 url 工作。

app_factory.py:

from flask import Flask
from flask_restful import Api

api = Api()

def create_app():
    app = Flask(__name__)    
    api.init_app(app)
    
    from resources.talk import Talk

    api.add_resource(Talk, '/talk')

    return app

run.py:

from app_factory import create_app
app = create_app()

127.0.0.1 - - [09/Jan/2021 22:07:09] "GET /talk HTTP/1.1" 404 127.0.0.1 - - [09/Jan/2021 22:07:09] “GET /talk HTTP/1.1”404

I also tried using我也尝试使用


def create_app():
    app = Flask(__name__)    
    api.init_app(app)
    
    from resources.talk import Talk
    with app.app_context():
        api.add_resource(Talk, '/talk')

    return app

but realized I'm just guessing and not understanding.但意识到我只是在猜测而不是理解。

Does anyone have any pointers on how to set this up correctly?有人对如何正确设置有任何指示吗? If I can' get this working then the JWT, database and other things stand no chance!如果我不能让它工作,那么 JWT、数据库和其他东西就没有机会了!

Thank you in advance.先感谢您。

I ran into the same problem.我遇到了同样的问题。 You have to bind the resources before initializing, like so:您必须在初始化之前绑定资源,如下所示:

from resources.talk import Talk
api.add_resource(Talk, '/talk')
api.init_app(app)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM