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 :

  • export_21_11_2021-151707.csv
  • export_22_11_2021-151707.csv
  • export_23_11_2021-151707.csv

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

  • export_23_11_2021-151707.csv
  • export_22_11_2021-151707.csv
  • export_21_11_2021-151707.csv

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.

score:1

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


Related Query