Flask route at / returns 404 when used with Flask-Restplus

徘徊边缘 提交于 2019-12-24 06:34:05

问题


I have a Flask app which has a Flask-RestPlus API as well as a "/" route. When I try to access "/" however, I get a 404. If I remove the Flask-RestPlus extension, the route works. How do I make both parts work together?

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)
api = Api(app, doc="/doc/")  # Removing this makes / work

@app.route("/")
def index():
    return "foobar"

回答1:


flask-restplus defines a different way of assigning routes according to their docs:

@api.route('/')
class Home(Resource):
    def get(self):
        return {'hello': 'world'}

Notice that the api variable is used instead of the app. Moreover, a class is used although I am not 100% sure it is required.




回答2:


This is an open issue in Flask-RestPlus. As described in this comment on that issue, changing the order of the route and Api solves the issue.

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)

@app.route("/")
def index():
    return "foobar"

api = Api(app, doc="/doc/")


来源:https://stackoverflow.com/questions/56539574/flask-route-at-returns-404-when-used-with-flask-restplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!