11
我試圖做一個函數來刪除我的數據庫中的記錄與瓶和SQLAlchemy的擴展。問題是,它並不是只刪除一行,而是刪除所有這些行。有人能告訴我我的代碼有什麼問題嗎?使用Flask-SQLAlchemy刪除行
@app.route('/admin/delete/<int:page_id>', methods=['GET','POST'])
@requires_auth
def delete_page(page_id):
page = Page.query.get(page_id)
if not page:
abort(404)
if page.children:
flash('You can not delete a page with child pages. Delete them, or assign them a different parent.',
'error')
return redirect(url_for('admin_page'))
if request.method == 'POST':
Page.query.get(page_id).query.delete()
db.session.commit()
flash('Page was deleted successfully', 'success')
return redirect(url_for('admin_page'))
return render_template('admin_delete.html', page_title=page.title, page_id=page_id)
在此先感謝!
煩人,實際上有一個錯誤(我已經嘗試過它)'AttributeError的:「頁」對象有沒有屬性「delete'' –
@tominated我現在就試試這個,並用解決方案更新我的答案。 –
是的,那修好了!謝謝! –