python - What should I do if I call the view function in the jinja2 template in the flask framework but do not want to redirect?
ringa_lee
ringa_lee 2017-05-18 10:57:04
0
2
741


I want to call the view function delete, but I don’t want to redirect to the delete page. Is this possible?

ringa_lee
ringa_lee

ringa_lee

reply all(2)
左手右手慢动作

According to my understanding, I think you want to delete without refreshing the page. If so, you need to use ajax. Use ajax to pass the id to the relevant processing view, and then get the passed id in the view. Delete, code:

function deleteUser(userid) {

    var post_data = {
        'userid': userid,
    }

    $.ajax({
        type: "POST",
        url: "/deleteuser",
        data: JSON.stringify(post_data, null, '\t'),
        contentType: 'application/json;charset=UTF-8',
        success: function(result) {
            // 传完数据之后做某些处理
            ...
        }
    });
}

View in flask:

@main.route('/deleteuser', methods=['POST'])
def delete_user():
    if request.method == 'POST':
        user_id = request.json['userid']
        user = User.query.get_or_404(user_id)
        db.session.delete(user)
        db.session.commit()
        return 'OK'// 这里你返回你要在页面上更新的数据,用来在上面的ajax里面的success部分做处理

This way you can delete the specified user without refreshing the page

某草草

If you don’t want to jump, you can consider using AJAX to access the URL of the deletion action. After the deletion is completed, refresh the current page.


The jump method you use, generally after deletion, you have to jump back to the current page in order to display the latest results

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template