[英]Cannot Resolve a 500 (Internal Server Error) Code
我遇到了一個 500 錯誤,我在練習制作具有各種列表的待辦事項應用程序時無法解決,並且這些待辦事項都具有完整的 CRUD 選項。
待辦事項代碼:
class TodoList (db.Model):
__tablename__ = 'todolists'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(), nullable=False)
todos = db.relationship('Todo', backref='list', lazy=True)
const todo_list_input = document.getElementById('todo_list_id');
document.getElementById('create_list_box_button').onsubmit = function(e) {
e.preventDefault();
const list_title = todo_list_input.value;
todo_list_input.value = '';
fetch('/todolists/create', {
method: 'POST',
body: JSON.stringify({
'todo_list_id': list_title,
}),
headers: {
'Contet-Type': 'application/json'
}
})
.then(response => response.json())
.then(jsonResponse => {
const li = document.createElement('li');
const checkbox = document.createElement('input');
checkbox.className = 'check-completed';
checkbox.type = 'checkbox';
checkbox.setAttribute('data-id', jsonResponse.id);
li.appendChild(checkbox);
const text = document.createTextNode(' ' + jsonResponse.description);
li.appendChild(text);
const deleteButton = document.createElement('button');
deleteButton.className = 'delete-button';
deleteButton.setAttribute('data-id', jsonResponse.id);
deleteButton.innerHTML = '✗';
li.appendChild(deleteButton);
document.getElementById('todo_lists_buttons').appendChild(li);
document.getElementById('error').className = 'hidden';
})
.catch(function() {
console.error('Error occurred');
document.getElementById('error').className = '';
})
}
@app.route('/todolists/create', methods=['POST'])
def create_todoList():
error = False
body = {}
try:
list_name = request.json()['list_name']
todoList = List(list_name=list_name)
db.session.add(todoList)
db.session.commit()
body['list_name'] = todoList.list_name
except:
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if not error:
return jsonify(body)
else:
abort(500)
我已經嘗試在整個過程中更改變量名稱,那里沒有任何樂趣,以及其他一些試圖查明問題的事情,並且對可能發生的事情沒有想法(在這方面仍然是新的)。
這僅在嘗試“創建”新的待辦事項列表時發生。 我可以在列表之間交換並創建/刪除待辦事項沒問題,只是創建列表不起作用。
待辦事項代碼:
class Todo(db.Model):
__tablename__ = 'todos'
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.String(), nullable=False)
completed = db.Column(db.Boolean, default=False)
list_id = db.Column(db.Integer, db.ForeignKey('todolists.id'), nullable=False)
const todo_item_input = document.getElementById('description');
document.getElementById('create_todo_box_button').onsubmit = function(e) {
e.preventDefault();
const desc = todo_item_input.value;
todo_item_input.value = '';
fetch('/todos/create', {
method: 'POST',
body: JSON.stringify({
'description': desc,
'list_id': {{ active_list.id }}
}),
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(jsonResponse => {
const li = document.createElement('li');
const checkbox = document.createElement('input');
checkbox.className = 'check-completed';
checkbox.type = 'checkbox';
checkbox.setAttribute('data-id', jsonResponse.id);
li.appendChild(checkbox);
const text = document.createTextNode(' ' + jsonResponse.description);
li.appendChild(text);
const deleteButton = document.createElement('button');
deleteButton.className = 'delete-button';
deleteButton.setAttribute('data-id', jsonResponse.id);
deleteButton.innerHTML = '✗';
li.appendChild(deleteButton);
document.getElementById('todos').appendChild(li);
document.getElementById('error').className = 'hidden';
})
.catch(function() {
console.error('Error occured');
document.getElementById('error').className = '';
})
}
@app.route('/todos/create', methods=['POST'])
def create_todo():
error = False
body = {}
try:
description = request.get_json()['description']
list_id = request.get_json()['list_id']
todo = Todo(description=description)
active_list = TodoList.query.get(list_id)
todo.list = active_list
db.session.add(todo)
db.session.commit()
body['description'] = todo.description
except:
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if not error:
return jsonify(body)
else:
abort(500)
我會嘗試在您的代碼中添加更詳細的異常。 另外,我認為您可以在 try...accept 中進行error
檢查
@app.route('/todos/create', methods=['POST'])
def create_todo():
body = {}
try:
description = request.get_json()['description']
list_id = request.get_json()['list_id']
todo = Todo(description=description)
active_list = TodoList.query.get(list_id)
todo.list = active_list
db.session.add(todo)
db.session.commit()
body['description'] = todo.description
return jsonify(body)
except Exception as e:
print(e) # ------- tracing
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
abort(500)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.