2014-11-16 5 views
0

코드템플릿에

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)) 

템플릿 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> 

질문 : 나는 django로 내 블로그를 만들고 있습니다. > ==되어 내가 메인 페이지에 그들에게 보여 보관소에 반복하려는하지만 난 결과 HTML 여기를 통해 코드를 실행하면 내가 사전

에 인스턴스 '속성에 도달 할 수

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

내가 무엇을 생각합니다 누락되었거나 잘못 되었습니까?

답변

1

dict.items 메서드를 사용할 수 있습니다. 파이썬 2.x에서는 dict.iteritems을 사용하는 것이 좋습니다.

{% 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 %} 
+0

감사합니다. 취업 (: – obayhan