Lolm_47
Lolm_47

Reputation: 25

django order csv using os.listdir

I'm a beginner in django, I'm trying to sort my csv by date In my view. I can export my schedules in csv and display them in my view but I don’t know how to sort them by date by the most recent one. But in My view not in my directory.

In my template it's Like this :

But I want to sort by date the most recent like this :

and where I show all export :

    def vm_schedule_download(request):
    media_url = settings.MEDIA_ROOT
    if not os.path.exists(f'{media_url}appli'):
        os.makedirs(f'{media_url}appli')
    if not os.path.exists(f'{media_url}appli/vm'):
        os.makedirs(f'{media_url}appli/vm')
    if not os.path.exists(f'{media_url}'
                          f'appli/vm/schedule_export'):
        os.makedirs(f'{media_url}appli/vm/schedule_export')
    filenames = os.listdir(f'{media_url}appli/vm/schedule_export')
    return render(request, 'appli/vm/vm_schedule_download.html',
                  {'filenames': filenames})

I tried sorted() but does nothink.

Upvotes: 1

Views: 86

Answers (1)

Lolm_47
Lolm_47

Reputation: 25

I found a solution using os.path.getmtime. There was already a solution in this topic stackoverflow.

Upvotes: 1

Related Questions