0

Code

engine.py ==>

class YearGroupCount(): def __init__(self, year, count, months): self.year = year self.count = count self.months = months class MonthGroupCount(): def __init__(self, month, count): self.month = month self.month_name = calendar.month_name[month] self.count = count class BlogCountsEngine(): def __init__(self): self.year_group_counts = {} def _add_date(self, year, month): if str(year) in self.year_group_counts: year_obj = self.year_group_counts[str(year)] else: year_obj = YearGroupCount(year, 0, {}) year_obj.count += 1 if str(month) in year_obj.months: month_obj = year_obj.months[str(month)] else: month_obj = MonthGroupCount(month, 0) month_obj.count += 1 year_obj.months[str(month)] = month_obj self.year_group_counts[str(year)] = year_obj def get_calculated_blog_count_list(self): if not Blog.objects.count(): retval = {} else: for blog in Blog.objects.all().order_by('-posted'): self._add_date(blog.posted.year, blog.posted.month) retval = self.year_group_counts return retval 

views.py ==>

def outer_cover(request): archives = BlogCountsEngine().get_calculated_blog_count_list() retdict = { 'categories': Category.objects.all(), 'posts': posts, 'archives': archives, } return render_to_response('blog/blog_list.html', retdict, context_instance=RequestContext(request)) 

template html ==>

 <div class="well"> <h4>Arşivler</h4> <ul> {% if archives %} {% for y_key, yr in archives %} <li>{{ yr.year }} &nbsp;({{ yr.count }})</li> {% for m_key,mth in yr.months %} <li>&nbsp;&nbsp; - {{ mth.month_name }} &nbsp; ({{ mth.count }})</li> {% endfor %} {% endfor %} {% endif %} </ul> </div> 

Question: I am building my own blog with django. I want to iterate in archives to show them in main page but i cannot reach instances' attributes in dictionary

When i run the code over in here the result html is ==>

<div class="well"> <h4>Arşivler</h4> <ul> <li> ()</li> </ul> </div> 

What am i missing or doing wrong?

1 Answer 1

1

You can use dict.items method. In python 2.x better use dict.iteritems instead.

{% for y_key, yr in archives.items %} <li>{{ yr.year }} &nbsp;({{ yr.count }})</li> {% for m_key, mth in yr.months.items %} <li>&nbsp;&nbsp; - {{ mth.month_name }} &nbsp; ({{ mth.count }})</li> {% endfor %} {% endfor %} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.