python,flask和jinja2:将参数传递给url_for

时间:2015-08-21 05:00:59

标签: python flask jinja2

我有这个:

{{ url_for('static', filename='css/main.min.css') }}

但我也希望传递模板中的时间戳以防止缓存,例如:

{{ url_for('static', filename='css/main.min.css?timestamp=g.timestamp') }}

这显然不起作用。 HTML需要最终阅读:

href="css/main.min.css?timestamp= 1440133238"

1 个答案:

答案 0 :(得分:3)

您可以使用类似this snippet的内容覆盖静态文件的from flask import Flask, render_template, request, url_for import os app = Flask(__name__) app.debug=True @app.context_processor def override_url_for(): return dict(url_for=dated_url_for) def dated_url_for(endpoint, **values): if endpoint == 'static': filename = values.get('filename', None) if filename: file_path = os.path.join(app.root_path, endpoint, filename) values['q'] = int(os.stat(file_path).st_mtime) return url_for(endpoint, **values) @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': app.run(port=5000) 处理程序。

这是一个有效的例子:

<强> app.py

<html>
<body>
<img src="{{ url_for('static', filename='20110307082700.jpg') }}" />
</body>
</html>

<强>模板/ index.html中

127.0.0.1 - - [21/Aug/2015 13:24:55] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [21/Aug/2015 13:24:55] "GET /static/20110307082700.jpg?q=1422512336 HTTP/1.1" 200 -

访问该页面时,访问日志中会显示以下内容:

    @model  IList<RoyaltyDb.Models.VerifyLicensorModel> 
    <table class="table">
        <tr>
            <th>
              Licensor
            </th>
            <th>
             Address
            </th>
            <th>
                Status
            </th>
            <th>
                Verify
            </th>
        </tr>
      @for (int i = 0; i < Model.Count(); i++) 
      {
            <tr>
                <td>         
                    @Html.HiddenFor(m => m[i].Licensor)                                                    
                    @Html.DisplayFor(m => m[i].Licensor)                                
                </td>
                <td>
                    @Html.TextAreaFor(m => m[i].Address)
                </td>
                <td>                              
                    @Html.LabelFor(m => m[i].IsVerified)                         
                    @Html.CheckBoxFor(m => m[i].IsVerified, new { @disabled = "disabled" })
                    <br />                  
                    @Html.HiddenFor(m => m[i].ActionId)
                    @Html.HiddenFor(m => m[i].ReferenceId)                                
                </td>
                <td>
                    <a onclick="SetProperties('@Model[i].Licensor')" class="btn">Verify</a>
                </td>
            </tr>
        }
    </table>


    <!-- Modal HTML -->
    <div id="VerifyLicensorModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">Verify  Licensor</h4>
                    <input type="hidden" id="targetPopup" />
                </div>
                <div class="modal-body" id="VerifyLicensorDetails">

                </div>
                <div class="modal-footer">
                    <a class="btn btn-primary" onclick="confirmLicensor()">Confirm</a>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
    function SetProperties(name)
    {
        //Showing a POPUp Here on element  VerifyLicensorModal
    }
    function confirmLicensor()
    {
       //Set the corresponding IsVerified checkbox to true
      //Set values of ActionId and ReferenceId params in the hidden fields 
      //ActionId  ReferenceId
    }