简体   繁体   English

访问资源烧瓶时的404

[英]404 when accessing resource Flask-Restful

from flask import g, request, session, render_template, flash, redirect, url_for
from flask import current_app as app

@app.route('/')
def home():
    return 'this works'


from flask_restful import Resource, Api
from app.extensions import api


class HelloWorld(Resource):
    def get(self):
        return {'Hello': 'World'}

api.add_resource(HelloWorld, '/test')  # Getting a 404 for this route on http://127.0.0.1:5000/test

Extensions sets up the api variable: 扩展程序设置api变量:

api = Api()
api.init_app(app)

I cannot figure out why I get a 404 when trying to access an api resource? 我无法弄清楚为什么在尝试访问api资源时会收到404错误?

Ok, the problem seems to be that the below ordering is wrong. 好的,问题似乎在于以下顺序是错误的。 I must add resources before I init the api. 在初始化api之前,我必须添加资源。

api = Api()
api.init_app(app)
api.add_resource(HelloWorld, '/')

Fix: 固定:

api = Api()
api.add_resource(HelloWorld, '/')
api.init_app(app)

This is quite strange given that eg SQLAlchemy needs to call init_app before it is used... 鉴于例如SQLAlchemy需要在使用init_app之前调用init_app,这是很奇怪的。

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

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