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