2013-07-24 26 views
2

我想列出目錄和子目錄中的文件。我已經使用this answer列表,但這些項目是不可點擊的,所以我想在文件名稱和位置之間添加一個鏈接。我試圖用這樣的東西修改模板:在帶有燒瓶的目錄中列出文件

<!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> 

但它不起作用,鏈接不好。 Wheareas我想鏈接到http://192.168.0.70:5000/static/repertory/subrepertory/file,我有一個鏈接http://192.168.0.70:5000/file,這導致了404。有人能幫助我嗎?

+0

相關:[如何使用Python生成html目錄列表](https://stackoverflow.com/q/10961378/4279) – jfs

回答

3

試試這個:

<ul><a href="/static/{{ loop(item.children) }}">{{ loop(item.children) }}</a></ul> 

我只是說,你需要直接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 

然後返回全路徑名,而不僅僅是文件名。

+0

我的確可以添加'/ static'。但是我不能添加'劇目/附屬曲目',因爲它是動態的(它們很少,不總是一樣)。 –

+0

因此,在模板中添加「/ static」,並在你的'make_tree'函數中添加劇目/無論如何! – HolgerSchurig