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.
Credit To: stackoverflow.com
Related Query
- django order csv using os.listdir
- Open the file in universal-newline mode using the CSV Django module
- Django model default sort order using related table field
- how to upload and read csv file in django using csv.DictReader?
- Specify order of columns in SELECT with UNION using Django ORM
- django download csv file using a link
- Using StreamingHttpResponse with Django Rest Framework CSV
- Ignore null values in descending order using Django Rest Framework
- Uploading and processing a csv file in django using ModelForm
- Using django and django-voting app, how can I order a queryset according to the votes of each item?
- Pylint doesn't report wrong import order when using Django
- Get records from a Django database using a list of IDs in a specific order
- Django : How to import a data from csv to database without using form?
- Using absolute function in Django query set order by clause
- How to validate contents of a CSV file using Django forms
- Is it possible to order a Django Model using the active language?
- Django change the order of pre_save post_save signals being called when using inlines?
- How do I order the import fields in admin using django import-export?
- Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order
- uploading csv file django using a form
- import CSV file into postgres using django
- parse and order csv file in django template
- Testing csv file stored in memory using unittest django
- Import csv file using django - form doesn't appear
- Django - order many to many relation on id of intermediary model without using through
- import csv file into mysql database using django web application
- How to upload a CSV file in Django using the REST API?
- How to use Django in order to present an old database without ruining it when using syncdb?
- Order queryset using the number of related objects in Django
- Django : How to upload csv file in unit test case using APIClient
More Query from same tag
- Why does apt-get install django 1.6.1 on Ubuntu 14.04?
- Django - Dynamic unique test for a model field
- django login with google app acoounts
- Django: IntegrityError when migrating after adding foreign key field
- Django - Wrong Validation Error Message when using self-written Validator
- Django applications path
- How does one incorporate a Django application into an existing twisted server?
- Passing django variables to javascript
- Django inclusion_tag issue
- HTTPS and HSTS on Apache/mod_wsgi and Django
- What is the difference between a QueryDict and MultiValueDict?
- Django Fake Primary Key
- Where can I download the Django documentation?
- django - initialize model fields including manytomanyfields
- Override signup view django-allauth