bin mit Flask schon seit 1 Monat beschäftigt aber seit gestern versuche ich das Beispiele hier https://flask-restful.readthedocs.io/en ... ll-example in meiner gebastelten (mit Hilfe eines Tuto)Webapp zu testen aber ich bekomme immer Fehler wegen nicht gefundene Modulen und möchte gern wissen wo kommt was hin. Vielen Dank
__init__.py
Code: Alles auswählen
.
.
.
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
from webapp.routes import PostList, Postss #doenst work
from routes import PostList, Postss #doesnt work
app = Flask(__name__)
api = Api(app)
#setup the Api resource routing here
api.add_resource(PostList, '/posts')
api.add_resource(Postss, '/posts/<post_id>')
Code: Alles auswählen
POSTS = {
'post1': {'task': 'Build an API'},
'post2': {'task': 'Test it'},
'post3': {'task': 'Deploy it, lol!'},
}
def abort_if_post_doesnt_exist(post_id):
if post_id not in POSTS:
abort(404, message="Post {} doesn't exist".format(post_id))
parser = reqparse.RequestParser()
parser.add_argument('task')
# Post
# shows a single post item and lets you delete a post item
class Postss(Resource):
def get(self, post_id):
abort_if_post_doesnt_exist(post_id)
return POSTS[post_id]
def delete(self, post_id):
abort_if_post_doesnt_exist(post_id)
del POSTS[post_id]
return '', 204 #for content
def put(self, post_id):
args = parser.parse_args()
task = {'task': args['task']}
POSTS[post_id] = task
return task, 201 #for created
# PostList
# shows a list of all posts, and lets you ADD POST to add new tasks
class PostList(Resource):
def get(self):
return POSTS
def addpost(self):
args = parser.parse_args()
post_id = int(max(POSTS.keys()).lstrip('post')) + 1
post_id = 'post%i' % post_id
POSTS[post_id] = {'task': args['task']}
return POSTS[post_id], 201
Code: Alles auswählen
from webapp import app
if __name__ == '__main__':
app.run(debug=True)
mignoncharly