Python Forum
Django serve list-file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Django serve list-file (/thread-5409.html)



Django serve list-file - foralobo - Oct-02-2017

Hello to everyone,
is there some package for Django to list the files in a directory and return a template ready to view them?
I would like to get more or less something like a node's serve-index package for Django.
It would by pretty to create it, but if there is something already ready... so good!!!

Does it exist something like that?

Thank you very much!!


RE: Django serve list-file - Larz60+ - Oct-02-2017

There is a method for traversing directories in os (import os)
this comes with the python installation
It's named os.walk().
This post has an example: https://python-forum.io/Thread-Automating-the-code-using-os-walk?highlight=walk


RE: Django serve list-file - nilamo - Oct-02-2017

There might be (I didn't see anything while googling, though), but it isn't a super great idea, since Django shouldn't be serving static files.  As Larz points out, it'd be trivial to build yourself using os.walk().

What might not be bad, would be to have a python script that's unrelated to Django, that runs once a day or so on a cron job, that just rebuilds an index.html file based on the current contents of the directory.  That way you've still got an index, it looks however you want, and you don't have to worry about django serving things it shouldn't.


RE: Django serve list-file - foralobo - Oct-03-2017

Thank you very much, the idea is not to serve static file, but to have an interface to view the list in a directory (like serve-index from npm for node).
It should be a template showing the list (i.e UI for a media server, nas etc). I can be created very fastly by hand, but if there is a ready solution is great.


RE: Django serve list-file - nilamo - Oct-03-2017

Even if there's not, serve-index is open source: https://github.com/expressjs/serve-index/tree/master/public

Copying the css/images/html, and just writing the four lines of python/django glue, should be pretty easy.