无法散列的类型:Flask 应用程序中的 'dict'
Unhashable type: 'dict' in Flask app
我正在学习有关如何在 Python 中使用 Flask 设计 REST API 的教程。它位于此处:
https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
一切顺利,直到 POST 部分。我精确地遵循了所有内容,但是我不断从 "return jsonify{'task', task})" 收到 TypeError "unhashable type: 'dict'"。我已经查看了示例代码和我自己的代码数十次,没有发现任何区别。这是我拥有的:
@app.route('/todo/api/v1.0/tasks', methods=['POST'])
def create_task():
if not request.json or not 'title' in request.json:
abort(400)
task = {
'id': tasks[-1]['id'] + 1,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task', task}), 201
鉴于本教程已有 4 年历史,当时发生了某些更改,导致示例因某种原因无效。我是 运行 它在 Python 2.7.3.
有关更多上下文,这是我的完整脚本:
from flask import Flask, jsonify, make_response, abort, request
app = Flask(__name__)
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
@app.errorhandler(400)
def bad_request(error):
return make_response(jsonify({'error': 'Bad request'}), 400)
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks})
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['GET'])
def get_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
abort(404)
return jsonify({'task': task[0]})
@app.route('/todo/api/v1.0/tasks', methods=['POST'])
def create_task():
if not request.json or not 'title' in request.json:
abort(400)
task = {
'id': tasks[-1]['id'] + 1,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task', task}), 201
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
谢谢。
正如 Pawel Kordowski 上面提到的,您想要 return 字典而不是集合:
return jsonify({'task': task}), 201
我正在学习有关如何在 Python 中使用 Flask 设计 REST API 的教程。它位于此处:
https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
一切顺利,直到 POST 部分。我精确地遵循了所有内容,但是我不断从 "return jsonify{'task', task})" 收到 TypeError "unhashable type: 'dict'"。我已经查看了示例代码和我自己的代码数十次,没有发现任何区别。这是我拥有的:
@app.route('/todo/api/v1.0/tasks', methods=['POST'])
def create_task():
if not request.json or not 'title' in request.json:
abort(400)
task = {
'id': tasks[-1]['id'] + 1,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task', task}), 201
鉴于本教程已有 4 年历史,当时发生了某些更改,导致示例因某种原因无效。我是 运行 它在 Python 2.7.3.
有关更多上下文,这是我的完整脚本:
from flask import Flask, jsonify, make_response, abort, request
app = Flask(__name__)
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
@app.errorhandler(400)
def bad_request(error):
return make_response(jsonify({'error': 'Bad request'}), 400)
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks})
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['GET'])
def get_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
abort(404)
return jsonify({'task': task[0]})
@app.route('/todo/api/v1.0/tasks', methods=['POST'])
def create_task():
if not request.json or not 'title' in request.json:
abort(400)
task = {
'id': tasks[-1]['id'] + 1,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task', task}), 201
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
谢谢。
正如 Pawel Kordowski 上面提到的,您想要 return 字典而不是集合:
return jsonify({'task': task}), 201