示例代码:
@app.route('/delete-update/<int:post_id>')
@login_required
def delete_update(post_id):
try:
p = Post.query.filter(Post.id == post_id).delete()
db.session.commit()
flash('Post successfully deleted')
except:
db.session.rollback()
return redirect(url_for('index'))
我可以通过单击按钮使用此路由删除帖子,但是如何防止通过请求URL删除帖子
localhost:5000 / delete-update / 1
答案 0 :(得分:0)
您应该在路由定义后使用methods
属性。
@app.route('/delete-update', methods=['POST'])
if request.method == 'POST':
#do your stuff
您还可以使用任何默认的http方法,例如PUT, DELETE, PATCH
。
请注意,如果您使用方法GET
向以下位置进行请求,则需要进行以下更改:
http://localhost:5000/delete-update/1
您将收到405
条消息method not allowed
的回复。
那是因为我们的路线不再处理GET
此外,您还应该更改按钮句柄以使用方法POST
向该路由发送请求。