Python 如何在django中提供静态文件列表?
是否可以通过模板提供django中给定目录中的静态文件列表。例如,www/example.com/files/path/to/file/应提供给定目录中所有文件的列表,而www/example.com/files/path/to/file/filename.ext应提供请求的文件您需要在视图中使用纯python实现或编写自己的模板标记来完成此操作 基本上你的目标是:Python 如何在django中提供静态文件列表?,python,django,templates,static,Python,Django,Templates,Static,是否可以通过模板提供django中给定目录中的静态文件列表。例如,www/example.com/files/path/to/file/应提供给定目录中所有文件的列表,而www/example.com/files/path/to/file/filename.ext应提供请求的文件您需要在视图中使用纯python实现或编写自己的模板标记来完成此操作 基本上你的目标是: import os def your_view(r): path = '/absolute/path/to/dir/'
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})
也许是这样的
您可以在模板标记中使用类似的方法