6

One seemingly basic thing that I'm having trouble with is rendering a simple list of static files (say the contents of a single repository directory on my server) as a list of links. Whether this is secure or not is another question, but suppose I want to do it... That's how my working directory looks like. And i want to list all the files fro analytics folder in my template, as links.

enter image description here
I have tried accessing static files in view.py following some tutorial 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)

And my template:

<p>ALL FILES:</p>
{% for file in allfiles %}
  {{ file }}
    <br>
{% endfor %}

And my 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"),
]

And i am getting the error:

FileNotFoundError at /analytics/
[WinError 3] The system cannot find the path specified: '/analytics'

Error traceback Any help will be very appreciated

Ilja Leiko
  • 426
  • 2
  • 7
  • 22
  • I think you misread the tutorial. ```static``` will generate a url for a static resource, not the file path. So you should probably traverse your filesystem and call static for each file to generate the link, which you should put into the web page – Alex Kreimer Oct 11 '17 at 15:05

3 Answers3

7

Came across a similar issue and found the following solution using django utilities:

from django.contrib.staticfiles.utils import get_files
from django.contrib.staticfiles.storage import StaticFilesStorage

s = StaticFilesStorage()
list(get_files(s, location='analytics'))
# ['analytics/report...', 'analytics/...', ...]
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • Sweet! Thanks for your answer. I ll accept it since it’s not relevant nowadays than my previously proposed approach. ;) – Ilja Leiko Nov 05 '19 at 15:25
  • Could you please provide some example for dummies, how to turn on showing of folders contents? E.g. now I have what was in tutorial and even don't use collectstatic: INSTALLED_APPS = [ ... 'django.contrib.staticfiles', in settings.py and from django.conf.urls.static import static if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL + 'static/', document_root=os.path.join(settings.MEDIA_ROOT, 'static')) in urls.py – Mikhail M Mar 21 '22 at 14:58
0

I don't have a django env to try this out, but try (something like) this:

def AnalyticsView(request):
    mypath = 'static'
    allfiles = [static(f) for f in listdir(mypath) if isfile(join(mypath, f))]

    return render_to_response('Rachel/analytics.html', allfiles)
Alex Kreimer
  • 1,106
  • 2
  • 11
  • 25
  • Thank you for you contribution, Alex. I have tried, but it still shown me the same error. "FileNotFoundError at /analytics/ [WinError 3] The system cannot find the path specified: 'static'" and argues about that line "allfiles = [static(f) for f in listdir(mypath) if isfile(join(mypath, f))] " – Ilja Leiko Oct 11 '17 at 15:30
  • This is probably because you didn't replace the ```mypath``` value :). – Alex Kreimer Oct 12 '17 at 11:37
0

It works! Here is a better approach:

import os

def AnalyticsView(request):
    path="E:\\Development\\Information_Access_Project\\Information_Access_Project\\static\\analytics"  # insert the path to your directory
    analytics_list =os.listdir(path)
    return render_to_response('Rachel/analytics.html', {'analytics': analytics_list})

and in template

{% for analytic in analytics %}
    <a href="/static/analytics/{{ analytic }}">{{ analytic }}</a>
    <br>
{% endfor %}
Ilja Leiko
  • 426
  • 2
  • 7
  • 22