flask 静态文件_在Flask开发过程中处理静态JS文件的简单方法

flask 静态文件

If your project has many JS files in the static directory, maintaining import links can be a pain.

如果您的项目在静态目录中有许多JS文件,那么维护导入链接可能会很麻烦。

This is a simple method to import all JS files from the static directory with three lines of code:

这是使用三行代码从静态目录导入所有JS文件的简单方法:

{% for file in js_files %}
<script type="text/javascript" src = { {file}}> </script>
{% endfor %}

Now I will explain how to generate and pass the js_files object to the front end.

现在,我将解释如何生成js_files对象并将其传递给前端。

Given a static folder organized like this each file would need to be imported using a url which can quickly become difficult to maintain.

给定一个像这样组织的静态文件夹,则每个文件都需要使用url导入,该URL可能很快变得难以维护。

First we need to import the listdir module.

首先,我们需要导入listdir模块。

import os
from os import listdir

The following function can be used on the backend to generate a list of urls.

可以在后端使用以下函数来生成URL列表。

def compile_javascript():    # Defining the path to the folder where the JS files are saved
path = 'static/javascript' # Getting all the files from that folder
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值