2013-05-09 37 views
0

是否可以通过模板在django中从给定目录提供静态文件列表。例如。 www/example.com/files/path/to/file /应该给出给定目录中所有文件的列表,www/example.com/files/path/to/file/filename.ext应该服务于请求的文件如何在django中提供静态文件列表?

回答

0

您需要在视图中使用纯Python实现,或者编写自己的模板标签来执行此操作。

基本上你喜欢的东西后:

import os 

def your_view(r): 
    path = '/absolute/path/to/dir/' 
    files = [f for f in os.listdir(path) if os.path.isfile(
     os.path.join(path, f))] 
    return render(request, 'path/to/template.html', { 
     'files': files}) 

像这样的事情吧?

您可以在模板标签中使用类似的方法。