【问题标题】:how to serve list of static files in django?如何在 django 中提供静态文件列表?
【发布时间】:2013-05-09 08:20:55
【问题描述】:

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

【问题讨论】:

    标签: python django templates static


    【解决方案1】:

    您需要在视图中使用纯 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})
    

    大概是这样的吧?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      • 2014-02-26
      • 2014-12-22
      相关资源
      最近更新 更多