python - List files in directories with flask -
i’d list files in directories , subdirectories. have used this answer have list, items non-clickables, i’d add link between name of files , locations. have try modify template :
<!doctype html> <title>path: {{ tree.name }}</title> <h1>{{ tree.name }}</h1> <ul> {%- 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>
but doesn’t work, links not good. wheareas want link http://192.168.0.70:5000/static/repertory/subrepertory/file
, have link http://192.168.0.70:5000/file
, leads 404. can me ?
try this:
<ul><a href="/static/{{ loop(item.children) }}">{{ loop(item.children) }}</a></ul>
i added static path need directly after href="
, before {{
.
another way can add add needed part of path in make_tree function already.
edit:
let make_tree() this:
def make_tree(path): tree = dict(name=path, children=[]) try: lst = os.listdir(path) except oserror: pass #ignore errors else: 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
then returns full path names , not file names.
Comments
Post a Comment