烧瓶按钮单击不生成POST消息

时间:2018-07-07 10:36:17

标签: ajax flask jinja2

我正在使用flask来生成一个网页。

该网页上有几个文本框和一个按钮。单击按钮时,我打算在同一页面中显示表格,其中包含从输入中获取的值。

Below is the flask code in python
===========================
    @app.route('/')
    def index():
    return render_template('index.html')


    @app.route('/ApplyFilter',methods=['POST','GET'])
    def ApplyFilter():       
        print("Hello")
        if request.method == 'POST':
            import  CompareDataset as cmp
            df3= cmp.CompareDataset()


             #conver the dataframe to the json objects
             AllRecords = json.loads(df3['both'].to_json(orient="records"))

             # count the number of records
             totalItems = df3['both']["Client"].count()
             client=request.form["client"]
             account= request.form["account"]
             auditstatus = request.form["auditstatus"]
             effectivedate=request.form["effectivedate"]
             cyclecode=request.form["cyclecode"]
             return   




     jsonify({'data':render_template('ApplyFilter.html',AllRecords=AllRecords,totalItems=totalItems)})

    return redirect('/')


snapshot of the html is as below

            <td>
                <a href=# id=applyfilter><button type="button" class="btn btn-primary active">Apply Filter</button></a>
            </td>
        </tr>
    </table>
    </div>
    </div>
    {% if  AllRecords %}
    <div class="container-fluid bg-3 text-left">
    <div class="counter"><span>Total Records Found : {{ totalItems }}</span></div>
    <div   "border-bottom: 1px solid #ddd; overflow-y: scroll;" >
    <table class="table" style="width: 90%; height: 100%;" >
    <thead style="color: #333; background-color: #f1f1f1">
      <tr>
        <th>Audit Status</th>
        <th>Client</th>
        <th>Effective Date</th>
        <th>Source System Code</th>
        <th>Account Number</th>
        <th>Cycle Code</th>
        <th>AuditTrailtimeStamp</th>
      </tr>
    </thead>
    <tbody>
    <tr class="success" >
        {% for key,value in AllRecords.iterrows() %}
        <td>{{ value["AuditStatusCode"] }}</td>
        <td>{{ value["Client"] }}</td>
        <td>{{ value["EffectiveDate"]  }}</td>
        <td>{{ value["SourceSystemCode"] }}</td>
        <td>{{ value["InternalAccountId"] }}</td>   
        <td>{{ value["CycleCode"] }} </td>
        <td>{{ value["AuditTrailtimeStamp_x"] }} </td>
    {% endfor %} 
    </tr>
    </tbody>
  </table>
  </div>
  </div>


    {% endif %}
    </form>


The java script tagged to button is as below

    $(function() {
    $('a#applyfilter').bind('click', function() {
    $.getJSON('/ApplyFilter', {
    client : $("client").val(),
    account : $("account").val(),
    auditstatus : $("auditstatus").val(),
    auditstatus : $("auditstatus").val(),
    effectivedate :$("effectivedate").val(),
    cyclecode : $("cyclecode").val(),
     }, function(data) {
    data = data.data;
    });
    return false;
    });
    });

On button click ApplyFilter is getting called. But it is not generating the POST message

    127.0.0.1 - - [07/Jul/2018 17:45:40] "GET / HTTP/1.1" 200 - Hello
    127.0.0.1 - - [07/Jul/2018 17:45:43] "GET /ApplyFilter HTTP/1.1" 302 -
    127.0.0.1 - - [07/Jul/2018 17:45:43] "GET / HTTP/1.1" 200 - Hello

applyfilter按钮不会生成后期调用,因此在路由Applyfilter中,if条件不能令人满意。

请协助并告知我所缺少的内容。

1 个答案:

答案 0 :(得分:0)

此代码存在许多问题,您仍然想对我要完成的工作感到有些困惑,但我现在将回答您的问题。

您的日志准确地告诉您问题出在哪里。您希望该if语句在发送发布请求时运行。但是您的日志显示您正在接收一个get请求并返回302,这表明您已被重定向,这是方法底部的情况。因此,如果您发送的是get请求,则不会触发if语句来检查您是否在使用post请求。

127.0.0.1 - - [07/Jul/2018 17:45:43] "GET /ApplyFilter HTTP/1.1" 302 -

当您应该使用类似getJson()方法的东西时,您似乎正在使用post()

但是还有其他问题,例如本节什么也没做

jsonify({'data':render_template('ApplyFilter.html',AllRecords=AllRecords,totalItems=totalItems)})

return redirect('/')

由于某种原因,您正在对对render_template的调用进行json处理,然后对其不执行任何操作?当该函数仅在Flask应用程序中有效时,我什至无法想到您通过json渲染render_template来实现的目标。如果可能的话,我可能会误会您,我很抱歉,请随时澄清一下,我会尽力提供帮助。

相关问题