Django. Listing files from a static folder

static files django
django static files not found in production
django serve static files in production
django static files 404
django static files not working
django nginx static files
django can't find static files
django static files tutorial

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.

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

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/...', ...]

Django. Listing files from a static folder, Came across a similar issue and found the following solution using django utilities: from django.contrib.staticfiles.utils import get_files from� This setting contains the address of the folder where we will be storing static files. When we execute the collectstatic command, the Django will store our files in this directory. The last is a list of directories – STATICFILES_DIRS. This list has file-paths of the folders where we store our static files.

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)

Working with Django Templates & Static Files ― Scotch.io, In this case, we just told Django to also look for static files in a folder all static files into a folder called staticfiles in our project root directory. Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files. For example:

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 %}

Django Static Files, How can I access (store/retrieve ) static file from the app_name/app_static_files( act as static for this app) folder ? Below is my project directory: -� Pandas - Dropping multiple empty columns. python,pandas. You can just subscript the columns: df = df[df.columns[:11]] This will return just the first 11 columns or you can do: df.drop(df.columns[11:], axis=1) To drop all the columns after the 11th one.

Handling Static Content in Django, In case 'django.contrib.staticfiles' is not listed, just add it for static files in the static directory of every installed app. collectstatic ¶ django-admin collectstatic¶. Collects the static files into STATIC_ROOT.. Duplicate file names are by default resolved in a similar way to how template resolution works: the file that is first found in one of the specified locations will be used.

Configuring Static Files in Django, The common practice is to have all the static files in a top-level static directory. STATICFILES_DIRS being a list indicates that having multiple static directories is � The pdfs could come from various places, but one of is a scanner. I thought about scanning into a django static folder and create instance of a IncomingFile Class when a new file would be detected. Then I can list the IncomingFiles.objects in a table and process them for distribution among users.

Django Static Files Handling made Simple, When we execute the collectstatic command, the Django will store our files in this directory. The last is a list of directories – STATICFILES_DIRS. {% load static %}, uses Django's special template tag syntax to tell the template engine to use the files in the static folder in this template. The static folder can be created anywhere in the

Comments
  • 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
  • Sweet! Thanks for your answer. I ll accept it since it’s not relevant nowadays than my previously proposed approach. ;)
  • 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))] "
  • This is probably because you didn't replace the mypath value :).
  • Hardcoding the the path isn't a good solution :/ You could probably use static\\analytics as path here.