1

I have built archives as such before on separate pages of my sites. However, Django does not want to cooperate with me this time it seems.

class IndexView(ArchiveIndexView):
    template_name = 'cms/blog_base.html'
    date_field = 'pub_date'
    latest = '20'
    model = Entry

In template {% for year in date_list %} {{ year|date:"Y" }} gives me a date relating to none of my entries. {% for entry in object_list % } {{ entry.pub_date|date:"Y" }} obviously outputs the correct date for the entry but as the entries grow I can only imagine it will continue to duplicate the years and months.

So what am I doing wrong? What do I have to do in ArchiveIndexView and template tag wise in order to relate the dates to my set of entries? In the past they were on separate pages and thus filtered by the regex in the url conf. One solution I saw was to create a custom manager using some raw SQL is that what I am looking at? If so I will just reconsider this all together. Thanks to the community in advance.

UPDATE: Example: What I am wanting on my home page is something similar to what is on this page https://unweb.me/blog/monthly-archives-on-Django I am also now thinking of trying their solution as it seems like a nice UI/UX. However, I am a simple person and would love to take the simple route if there is one.

2 Answers 2

3

Are you looking for the regroup-feature?

Example:

{% regroup object_list by date_field|date:"Y" as year_list %}
{% for year in year_list %}
    {% regroup year.list by date_field|date:"F" as month_list %}
    {% for month in month_list %}
        {{ month.grouper }} / {{ year.grouper }} <br />
        {{ month.list }}
    {% endfor %}
{% endfor %}
5
  • I will try this. I think the problem is that I am wanting it all on the front page so without any arguments from urlconf, a custom manager, or a complex view I'm going to have trouble. I will try your solution and let you know. Thanks :)
    – eusid
    Sep 28, 2012 at 7:45
  • Yeah this does what I want but I am not sure how it will behave as the entries grow. I just want a list of years(not clickable) with links beneath them linking to the monthly archive of that year. I'm assuming the problem I was having before was that my Entry model was not grouped into the dates? It showed a date from 2011 which I assume is the date of some object in the object_list. I do have date_field specified in my subclass of ArchiveIndexView. I want something like is on the right column on this page, also thinking about using their sol. unweb.me/blog/monthly-archives-on-Django
    – eusid
    Sep 28, 2012 at 9:24
  • I use this for a timelogging-system with about 85 users & each got ~8 entries per workday. I'm showing the last 6 months - so there are around 960 entries in the object_list. Sep 28, 2012 at 10:34
  • For the 'count' behind the months I'd use my own template tag. something like: {% sumMonth month.grouper %} // I could create a better example later today (little bit busy at the moment - sorry). Sep 28, 2012 at 10:36
  • yeah, the solution is so simple when you link to the archives by year as the urlconf takes care of most of it. i've seen two methods one is the one i linked to which is a nice UI element to have imo. The other was a fairly complex custom manager. I might just scrap the archives on the side and just have my tag cloud. Leave archives as a link at the top-nav list in the view filter by date passed in with url parameter -- which can easily be done by over-riding a method of listview.
    – eusid
    Sep 28, 2012 at 15:26
2

A better solution I have found online from a short blog tutorial implements the following. I verify that it works.

def mkmonth_lst():
"""Make a list of months to show archive links."""
if not Post.objects.count(): return []

# set up vars
year, month = time.localtime()[:2]
first = Post.objects.order_by("created")[0]
fyear = first.created.year
fmonth = first.created.month
months = []

# loop over years and months
for y in range(year, fyear-1, -1):
    start, end = 12, 0
    if y == year: start = month
    if y == fyear: end = fmonth-1

    for m in range(start, end, -1):
        months.append((y, m, month_name[m]))
return months

def main(request):
"""Main listing."""
posts = Post.objects.all().order_by("-created")
paginator = Paginator(posts, 10)
try: page = int(request.GET.get("page", '1'))
except ValueError: page = 1

try:
    posts = paginator.page(page)
except (InvalidPage, EmptyPage):
    posts = paginator.page(paginator.num_pages)

return render_to_response("list.html", dict(posts=posts, user=request.user,
                                            post_list=posts.object_list,    months=mkmonth_lst()))

# The template info
    <div id="sidebar">
    Monthly Archive
    <p>
    {% for month in months %}
        {% ifchanged month.0 %} {{ month.0 }} <br /> {% endifchanged %}
        <a href="{% url blog.views.month month.0 month.1 %}">{{ month.2 }}</a> <br />
    {% endfor %}
    </p>
</div>

This will generate a year separated list on your homepage with months that only contain posts.

2
  • This includes all years/months, regardless of post. Do you know how to make it only output years/months for which there is a post? Feb 18, 2015 at 21:45
  • I had to import time and import calendar to make this work.
    – woodbine
    Jul 7, 2016 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.