Flask,动态生成文件链接

Bob*_*uba 1 python flask

如何使用flask动态生成文件链接?这是我的示例代码,但是当我点击链接时它不起作用.目录中的所有文件都是文本文件,因此浏览器可以打开它们而无需下载任何内容.

{% for x, y in links %}
<tr>
     <td><a href="{{y}}">{{x}}</a></td>
     <td><a href="{{y}}">{{y}}</a></td>
</tr>
{% endfor %}

path = 'path/directory'
if request.method == 'GET':
# a is the date last modified, b is the file path
    link = [(a, b) for a, b in get_files_sorted(path)]
    return render_template('file_structure.html', links=link)
if request.method == 'POST':
    #open the text file that was clicked on
Run Code Online (Sandbox Code Playgroud)

小智 5

假设您的文件位于静态目录中,您可以执行以下操作:

<td><a href="{{ url_for('static', filename=y) }}">{{x}}</a></td>

要么

<td><a href="{{ url_for('static', filename='path/directory/%s' % (y)) ) }}">{{x}}</a></td>