【问题标题】:Flask Button click not generating POST message烧瓶按钮单击不生成 POST 消息
【发布时间】:2018-07-07 10:36:16
【问题描述】:

我正在使用烧瓶生成单个网页。

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

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 按钮没有生成 post call,因此在路由 Applyfilter 中,if 条件不满足。

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

【问题讨论】:

    标签: ajax flask jinja2


    【解决方案1】:

    这段代码有很多问题,我仍然对你想要完成的工作感到有些困惑,但我现在会回答你的问题。

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

    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 应用程序中起作用时,我什至无法想到您通过 jsonifying render_template 来实现什么。我可能只是误解了你,如果我是这样道歉,请随时进一步澄清,我会尽我所能提供帮助。

    【讨论】:

    • 感谢蒂姆的回复。
    • 感谢蒂姆的回复。我已经把它复杂化了。我的要求是当按钮被点击时将数据帧显示到 sam 页面上。数据帧不能被序列化,因此使用 jsonify 和 json.loads 来删除索引并转换为 json。 .我仅在请求发布时重新使用 jsonify,否则重定向到同一页面。请告知是否有一种简单的方法来执行此操作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 2013-11-16
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2010-11-16
    • 2017-06-26
    相关资源
    最近更新 更多