Login

very archive view

Author:
stuntgoat
Posted:
October 19, 2009
Language:
Python
Version:
1.1
Score:
2 (after 2 ratings)

I tried to think of a way to use generic views to show all my blog posts on one page organized chronologically with each post title under one month name and all the month names under one year name.

Like so:

`

2009

October

    My last example blog entry

    Entry even before the last one

    First of October

September

    Only one entry in Sept.

2008

December

    It is cold in December

    First posting of my blog

`

I could not think of a way to do this using generic views, before I wrote a view and some template logic that does.

The template logic is here:

http://www.djangosnippets.org/snippets/1765/

Any suggestions for design patterns are greatly appreciated.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def create_archive_list(_query):
    """
    given a sorted queryset, return a list in the format:
    
    archive_list = [{'2009': [{'December': ['entry1', 'entry2']},
                              {'January': ['entry3', 'entry4']}
                              ]
                     },
                    
                    {'2008': [{'January': ['entry5', 'entry6']},
                              ]
                     }
                    ]
    """

    archive_list = []
    tmp_months_list = []
    tmp_year_list = []

    for item in _query:
        if item.pub_date.year not in tmp_year_list:
            tmp_year_list.append(item.pub_date.year)
            tmp_dict = {}
            tmp_dict[item.pub_date.year] = []
            archive_list.append(tmp_dict)
        else:
            pass

    for entry in _query:
        for item in archive_list:
            _year = entry.pub_date.year
            if _year in item:
                _tmp_month = entry.pub_date.strftime("%B")
                # make a dictionary with the month name as a key and an empty list as a value
                tmp_dict = {}
                tmp_dict[_tmp_month] = []

                if tmp_dict not in item[_year]:
                    item[_year].append(tmp_dict)
                
                    
    for entry in _query:
        # dict in list
        for item in archive_list:
            # year of entry
            _year = entry.pub_date.year
            if _year in item:
                _year_list = item[_year]
                for _dict_month in _year_list:
                    _tmp_month = entry.pub_date.strftime("%B")
                    if _tmp_month in _dict_month:
                        _dict_month[_tmp_month].append(entry)
                        
    return archive_list


def very_archive(request):
    query = Entry.objects.all().order_by('-pub_date')
    object_years_dict = create_archive_list(query)
    
    return render_to_response('ideas/ideas_index.html', {'archive_list': object_years_dict})

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.