Python AttributeError:“dict”对象没有属性“\u\u name\u”
我正在尝试运行一个Flask脚本,但得到以下错误AttributeError:“dict”对象没有属性“\uuuuu name\uuuuuu”。如何解决此错误 编辑:我添加了entry_views.py和entry_models.py文件。我希望它们能更清楚地说明这个问题 这是错误日志 这些是我正在使用的文件 run.pyPython AttributeError:“dict”对象没有属性“\u\u name\u”,python,flask,Python,Flask,我正在尝试运行一个Flask脚本,但得到以下错误AttributeError:“dict”对象没有属性“\uuuuu name\uuuuuu”。如何解决此错误 编辑:我添加了entry_views.py和entry_models.py文件。我希望它们能更清楚地说明这个问题 这是错误日志 这些是我正在使用的文件 run.py import os from werkzeug.contrib.fixers import ProxyFix from application import create
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)
init.pycreate_应用程序
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
条目_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>')
你的代码应该是这样的小写字母d吗
api = Namespace('entries', description='Operations on entries')
而不是:
api = Namespace('entries', Description='Operations on entries')
看一下,这似乎是区别那是什么application.views.entry\u视图?看起来您将其作为flask_restplus.Namespace传递,但实际上它只是一个dict.@abarnert,我已添加了该文件。Thank是否可以尝试将名称空间重命名为其他名称?这是Entries资源还是空dict?api.add_resourceentries,“/entries”
api = Namespace('entries', Description='Operations on entries')