问题
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