基于url的脚本条件插入脚本

时间:2017-10-25 21:56:48

标签: python flask

我想使用flask的内置if else模板标签有条件地添加脚本

如果我在我的资助页面上,我想插入我的资金脚本

例如:

{% if url == '/funding' %}
    <script src="{{ url_for ('static', filename='js/funding.js')}}"></script>
{% endif %}

{% if url_for == 'funding' %}
        <script src="{{ url_for ('static', filename='js/funding.js')}}"></script>
{% endif %}

设置上下文会抛出错误

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/darren/PycharmProjects/schoolDonations/schoolDonations.py", line 22, in funding
    return render_template("funding.html", url_for='funding')
  File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 134, in render_template
    context, ctx.app)
  File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 116, in _render
    rv = template.render(context)
  File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/darren/PycharmProjects/schoolDonations/templates/funding.html", line 1, in top-level template code
    {% extends "index.html" %}
  File "/Users/darren/PycharmProjects/schoolDonations/templates/index.html", line 5, in top-level template code
    <link rel="stylesheet" href="{{ url_for ('static',
AttributeError: 'str' object has no attribute '__call__'

1 个答案:

答案 0 :(得分:0)

你显然可以这样做。我假设您在多个路由中使用相同的模板,并希望在特定路由中加载JS文件。你可以这样做:

application.py

from flask import Flask
from flask import request
from flask import render_template

app = Flask(__name__)

@app.route('/')
def show_index():
    return render_template('funding.html')

@app.route('/funding')
def show_funding():
    return render_template('funding.html')

if __name__ == '__main__':
    app.run(debug = True) 

我们可以在模板中使用{{ request.path }},它返回当前请求的相对路径。模板funding.html包含:

<!DOCTYPE html>
<html>
<head>
    <title>Template</title>
</head>
<body>
    <h3>You are here: {{ request.path }}</h3>
    {% if request.path == url_for('show_funding') %}
        <script src="{{ url_for ('static', filename='js/funding.js')}}"></script>
    {% endif %}
</body>
</html>

JS文件(funding.js)位于js文件夹中的static。它包含一条警报消息:

alert("Hello from funding page");

现在,当我们访问根路径时,JS文件未加载,但是当我们访问funding路径时,它正在加载如下:

load js dynamically in template