【问题标题】:Can't link js file to html file in flask template无法将 js 文件链接到烧瓶模板中的 html 文件
【发布时间】:2016-07-25 17:01:25
【问题描述】:

在我的项目中有一个名为 mine 的文件夹,其中包含 2 个子文件夹(静态文件夹和模板文件夹)和 1 个 app.py 文件 init。

我已将我的 jQuery 脚本文件放在静态文件夹中,而 shop.html 文件位于模板文件夹中。我想将该 html 文件链接到该 js 文件。

我把这个放在 html 文件中:

<script type=text/javascript src="{{url_for('static', filename='/static/hide.js') }}"></script>

但它不起作用?有什么问题?

【问题讨论】:

    标签: javascript jquery html templates flask


    【解决方案1】:

    你有一个重复的'static'

    url_for('static', filename='hide.js')
    

    第一个'static' 将自动填充指向静态文件夹的URL,因此将'static' 放在filename 字段中是多余的。

    url_for('static', filename='static/hide.js')
    # '/static/static/hide.js'
    
    url_for('static', filename='hide.js')
    # '/static/hide.js'
    

    【讨论】:

    • 是的,我知道,但 &lt;script type=text/javascript src="{{url_for('static', filename='hide.js') }}"&gt;&lt;/script&gt; 对我不起作用,因为我改变了路径。
    • 哦,是的,我发现了问题,只是一个小错误。不过谢谢你的解释。
    猜你喜欢
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    相关资源
    最近更新 更多