簡體   English   中英

如何在Django中提供靜態文件列表?

[英]how to serve list of static files in django?

是否可以通過模板從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/'
    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})

大概是這樣嗎?

您可以在模板標記中使用類似的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM