render_template()无效,无法使用新数据更新页面

时间:2015-10-27 05:18:50

标签: javascript jquery python ajax flask

我正在使用Flask编写Web应用程序。

服务器代码

@app.route('/load')
def load(): 
    return render_template("next.html",message = {"date_to":"from","date_from":tmp,"error_stat":[30,400,21,45],"mac":[["1.1.1.1","1.2.1.2","3.2.1.1","3.1.4.5","1.3.2.4","5.5.4.3","2.1.6.7"],                                                  ["6.1.1.1","1.2.3.2","3.2.1.1","3.2.4.5","1.3.2.4","5.5.4.3","2.1.6.7"],                                                    ["9.1.1.1","4.2.1.2","3.2.1.1","3.7.4.5","1.3.2.4","5.5.4.3","2.1.6.7"],                                                    ["10.1.1.1","1.2.1.2","3.2.1.1","3.6.4.5","1.3.2.4","5.5.4.3","2.8.6.7"]],                                                  "ip":[["1.1.1.1","1.2.1.2","3.2.1.1","3.1.4.5"],                                                ["6.1.1.1","3.2.4.5","1.3.2.4","5.5.4.3","2.1.6.7"],                                                    ["9.1.1.1","4.2.1.2","3.2.1.1","3.7.4.5","1.3.2.4","5.5.4.3","2.1.6.7"],                                                    ["10.1.1.1","3.6.4.5","1.3.2.4","5.5.4.3","2.8.6.7"]]                                           })

@app.route('/submitdate',methods=['GET'])
def submitdate():
    tmp1 = request.form.get('start_date')
    tmp2 = request.form.get('end_date')
    return render_template("next.html",message ={"date_to":tmp2,"date_from":tmp1,"error_stat":[130,100,121,145],"mac":[["4.1.1.1","1.2.1.2","3.2.1.1","3.1.4.5","1.3.2.4","5.5.4.3","2.1.6.7"],                                                 ["5.1.1.1","1.2.3.2","3.2.1.1","3.2.4.5","1.3.2.4","5.5.4.3","2.1.6.7"],                                                    ["6.1.1.1","4.2.1.2","3.2.1.1","3.7.4.5","1.3.2.4","5.5.4.3","2.1.6.7"],                                                    ["7.1.1.1","1.2.1.2","3.2.1.1","3.6.4.5","1.3.2.4","5.5.4.3","2.8.6.7"]],                                                   "ip":[["8.1.1.1","1.2.1.2","3.2.1.1","3.1.4.5"],                                                    ["9.1.1.1","3.2.4.5","1.3.2.4","5.5.4.3","2.1.6.7"],                                                    ["0.1.1.1","4.2.1.2","3.2.1.1","3.7.4.5","1.3.2.4","5.5.4.3","2.1.6.7"],                                                    ["1.1.1.1","3.6.4.5","1.3.2.4","5.5.4.3","2.8.6.7"]]                                                    })

JQuery前端

$('#buttonid').on('click', function (e) {
    $.get("/submitdate",{"start_date":$('#start').val(),"end_date":$('#end').val()},function(data,status)
    {              
            //location.reload(true);(Can I do this to reload the page with new data???)        

    }); 
});

我有一个ID为“buttonid”的按钮,当我点击它时,它应该使用GET将数据发送到服务器。然后,页面应该加载新数据。在我的HTML代码中,我从服务器访问数据{{message.error_stat[0]}}(例如)。我第一次加载页面时没有任何问题。但是,当我单击按钮提交数据并使用GET获取新数据时,我无法使用新数据进行更新。语法{{message.error_stat[0]}}无效。我可以知道我哪里出错吗?

1 个答案:

答案 0 :(得分:0)

你可以这样做

$('#buttonid').on('click', function (e) {
   window.location.href = '/submitdate?start_date='+$('#start').val()+'&end_date='+$('#end').val()
});

然后,

tmp1 = request.args.get('start_date')
tmp2 = request.args.get('end_date')