我正在尝试运行Flask脚本,但是出现以下错误AttributeError: 'dict' object has no attribute '__name__'
。如何解决此错误?
编辑:我已经添加了entry_views.py和entry_models.py文件。希望它们可以使问题更加清晰
这些是我正在使用的文件
run.py
import os
from werkzeug.contrib.fixers import ProxyFix
from application import create_app
if __name__ == '__main__':
app = create_app('default')
app.wsgi_app = ProxyFix(app.wsgi_app)
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port, debug=True)
初始化 .py(create_app)
from flask import Flask, Blueprint
from flask_restplus import Api
from instance.config import configuration
def create_app(config):
app = Flask(__name__, instance_relative_config=True)
app.config.from_object(configuration[config])
app.url_map.strict_slashes = False
# Enable swagger editor
app.config['SWAGGE_UI_JSNEDITOR'] = True
# initialize api
api = Api(app=app,
title='My Diary',
doc='/api/v1/documentation',
description='A Simple Online Diary.')
#doc = ('/api/v1/documentation')
from application.views.entry_views import api as entries
# Blueprints to be registered here
api.add_namespace(entries, path='/api/v1')
return app
entry_views.py
from flask_restplus import Resource, Namespace, fields
from flask import request, jsonify
from datetime import datetime
from application.models.entry_models import DiaryEntry
api = Namespace('entries', Description='Operations on entries')
# data structure to store entries
entries = {}
entry = api.model('entry', {
'title': fields.String(description='location of the driver'),
'body': fields.String(description='end-point of the journey')
})
class Entries(Resource):
@api.doc(responses={'message': 'entry added successfully.',
201: 'Created', 400: 'BAD FORMAT'})
@api.expect(entry)
def post(self):
"""creates a new diary entry."""
data = request.get_json()
# Check whether there is data
if any(data):
# save entry to data structure
# set id for the entry offer
entry = DiaryEntry(data)
entry_id = len(entries) + 1
entries[(entry_id)] = entry.getDict()
response = {'message': 'entry offer added successfully.',
'offer id': entry_id}
return response, 201
else:
return {'message': 'make sure you provide all required fields.'}, 400
@api.doc('list of entries', responses={200: 'OK'})
def get(self):
"""Retrieves all available entries"""
return (entries)
api.add_resource(entries, '/entries')
class Singleentry(Resource):
@api.doc('Get a single entry',
params={'entry_id': 'Id for a single entry offer'},
responses={200: 'OK', 404: 'NOT FOUND'})
def get(self, entry_id):
"""Retrieves a single entry."""
try:
entry = entries[int(entry_id)]
entry['id'] = int(entry_id)
return jsonify(entry)
except Exception as e:
return {'message': 'entry does not exist'}, 404
api.add_resource(Singleentry, '/entries/<string:entry_id>')
entry_models.py
from datetime import datetime
class DiaryEntry(object):
"""
Model of a diary entry
title: title of the diary entry,
body: body of the diary entry
"""
def __init__(self, data):
self.title = data['title']
self.body = data['body']
def getDict(self):
return self.__dict__
答案 0 :(得分:1)
您的代码应像这样(小写d)
api = Namespace('entries', description='Operations on entries')
而不是:
api = Namespace('entries', Description='Operations on entries')
看着code examples 似乎与众不同