我遇到的一件看似基本的事情是将一个简单的静态文件列表(例如我的服务器上单个存储库目录的内容)呈现为链接列表。这是否安全是另一个问题,但假设我想这样做......
这就是我的工作目录的样子。我想将模板中分析文件夹的所有文件作为链接列出。
I have tried accessing static files in view.py following some tutorial https://stackoverflow.com/questions/11721818/django-get-the-static-files-url-in-view and having it like that:
view.py
from os import listdir
from os.path import isfile, join
from django.contrib.staticfiles.templatetags.staticfiles import static
def AnalyticsView(request):
mypath = static('/analytics')
allfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
return render_to_response('Rachel/analytics.html', allfiles)
还有我的模板:
<p>ALL FILES:</p>
{% for file in allfiles %}
{{ file }}
<br>
{% endfor %}
还有我的settings.py
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
我收到错误:
FileNotFoundError at /analytics/
[WinError 3] The system cannot find the path specified: '/analytics'
Error 追溯 http://dpaste.com/2GBKR9T任何帮助将不胜感激