I have a processors.py file where I generate a context processor to load static data into my project's base template. I have posts and I want to generate as a kind of archive in a aside, where you can access through a link, the posts that were created in the month of August 2022 (for example) and that will take me to a page where I will see the entire list of posts. To build that list, from this kind of file, I do the following in the processors.py file:
def ctx_dic_history(request):
ctx_history = {}
ctx_history['dates'] = Post.objects.dates('created','month').distinct() return ctx_history
If I put {{ dates }} in the aside, it returns the following:
<QuerySet [datetime.date(2022, 7, 1), datetime.date(2022, 8, 1), datetime.date(2022, 9, 1), datetime.date(2022, 10, 1)]>
Now I want to go through that queryset, but I can't get what I need: I want them to be sorted descending first, that is, the most recent month on top. Second, I want to access the month and the year, that is, something like "August 2022" appears.
{% for item in dates %}
<a href="">Probando</a>
{% endfor %}
With that I can iterate over the items that this queryset brings me, but I don't know how to access the date in question for each item in the queryset. If you can give me a hand I would appreciate it in advance. If I manage to get it to work, I'll definitely post the answer that worked for me. Thank you so much