Reputation: 1084
Using os.listdir()
to get files of a folder and os.remove()
to remove files from it and files are still listed after being removed until Django server is restarted. Is it any way to avoid having to restart the server to get the "correct" file list?
Have a file called pics.py
with a file_list
that is passed via views.py
and rendered in a template.
The error appear when running from browser, via eclipse the list is updated instantly.
from os import listdir
from os.path import isfile, join, dirname, abspath
mypath = dirname(dirname(abspath(__file__))) + "\..\media\dump"
file_list = [f for f in listdir(mypath) if isfile(join(mypath, f))]
Upvotes: 0
Views: 721
Reputation: 169328
You will need to update file_list
in your view function, e.g.
from os import listdir
from os.path import isfile, join, dirname, abspath
def get_files():
mypath = dirname(dirname(abspath(__file__))) + "\..\media\dump"
return [f for f in listdir(mypath) if isfile(join(mypath, f))]
def my_view(request):
return render(request, "template.html", {"files": get_files()})
(or the equivalent class-based view).
Upvotes: 1