Home  >  Q&A  >  body text

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?


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

ringa_leeringa_lee2711 days ago660

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-05-18 10:59:04

    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

    reply
    0
  • 某草草

    某草草2017-05-18 10:59:04

    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

    reply
    0
  • Cancelreply