我想列出目录和子目录中的文件。我已经使用这个答案来获得列表,但这些项目是不可点击的,所以我想在文件名和它们的位置之间添加一个链接。我试图用这样的东西修改模板:
<!doctype html>
<title>Path: {{ tree.name }}</title>
<h1>{{ tree.name }}</h1>
<ul>
{%- for item in tree.children recursive %}
<li><a href="{{ item.name }}">{{ item.name }}</a>
{%- if item.children -%}
<ul><a href="{{ loop(item.children) }}">{{ loop(item.children) }}</a></ul>
{%- endif %}</li>
{%- endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
但它不起作用,链接不好。我想要一个指向 的链接http://192.168.0.70:5000/static/repertory/subrepertory/file,我有一个指向http://192.168.0.70:5000/file404的链接。有人可以帮助我吗?
尝试这个:
<ul><a href="/static/{{ loop(item.children) }}">{{ loop(item.children) }}</a></ul>
Run Code Online (Sandbox Code Playgroud)
我只是在href=", 之后直接添加了您需要的静态路径{{。
您可以执行此操作的另一种方法是在 make_tree 函数中添加添加所需的路径部分。
编辑:
让 make_tree() 看起来像这样:
def make_tree(path):
tree = dict(name=path, children=[])
try: lst = os.listdir(path)
except OSError:
pass #ignore errors
else:
for name in lst:
fn = os.path.join(path, name)
if os.path.isdir(fn):
tree['children'].append(make_tree(fn))
else:
tree['children'].append(dict(name=fn))
return tree
Run Code Online (Sandbox Code Playgroud)
然后它返回完整的路径名,而不仅仅是文件名。
| 归档时间: |
|
| 查看次数: |
17327 次 |
| 最近记录: |