Ich folgte der official docs von flask-RESTful
und ich versuche, die erste Hallo Welt Demo zu implementieren.Python - Flask-RESTful 404 Fehler bekommen?
Zuerst legte ich den gesamten Beispielcode in eine einzige Datei und alles funktioniert gut.
Wenn ich jedoch den Code in drei getrennte Dateien aufteilte (um das Projekt strukturierter zu gestalten), bekam ich immer einen 404 Fehler.
Datei stucture
.
├── app.py
├── app
├── __init__.py
├── __api__.py
└── venv
__init__.py
# -*- coding: utf-8 -*-
#initialization
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
api.py
# -*- coding: utf-8 -*-
from app import app, api
from flask_restful import Resource, Api
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/')
app.py
# -*- coding: utf-8 -*-
from app import app
if __name__ == '__main__':
app.run(debug=True)
In Python Konsole:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 250-643-552
127.0.0.1 - - [03/May/2016 22:35:20] "GET/HTTP/1.1" 404 -
127.0.0.1 - - [03/May/2016 22:35:24] "GET/HTTP/1.1" 404 -
127.0.0.1 - - [03/May/2016 22:38:15] "GET/HTTP/1.1" 404 -
Was ist los?
EDIT
Ich kann jetzt das erwartete Ergebnis durch Bewegung erhalten api.add_resource(HelloWorld, '/')
zu app.py
app.py (bearbeitet)
# -*- coding: utf-8 -*-
from app import app, api
from app.api import HelloWorld
api.add_resource(HelloWorld, '/')
if __name__ == '__main__':
app.run(debug=True)
Kann nicht herausfinden, warum?