Python 为博客创建归档列表

Python 为博客创建归档列表,python,flask,sqlalchemy,jinja2,Python,Flask,Sqlalchemy,Jinja2,我正在尝试使用SQLAlchemy、Flask和Jinja创建一个包含计数的归档列表,如下所示。我不想展示没有帖子的月份。我搞不懂如何在字典中找到每一年、每一个月的唯一年份、月份和计数 2012 (11) - January (3) - February (5) - April (3) 2013 (14) - April (2) - May (8) - June (2) - December 2014 (13) - January (3) - February (8)

我正在尝试使用SQLAlchemy、Flask和Jinja创建一个包含计数的归档列表,如下所示。我不想展示没有帖子的月份。我搞不懂如何在字典中找到每一年、每一个月的唯一年份、月份和计数

 2012 (11)
 - January (3)
 - February (5)
 - April (3)
 2013 (14)
 - April (2)
 - May (8)
 - June (2)
 - December
 2014 (13)
 - January (3)
 - February (8)
 - March (2)
模型 更新

这段代码创建了字典,但不确定如何添加计数。有什么想法吗?有更好的方法吗

观点

jinja2模板

解决方案 以下是答案中最初发布的解决方案

@app.route('/archive')
def display_archive():
    p = db.session.query(Post.pub_date).all()
    d = defaultdict(list)
    for i in p:
        d[i.pub_date.year].append(i.pub_date.month)

    # add the monthly counts by counting the instances of month number.
    adict = {}
    for k, v  in d.items():
        adict[k] = Counter(v)

    bdict = {}
    # add the monthly and yearly totals counts
    posttotal = 0
    for key, value in adict.iteritems():
        yearsum = 0
        for m, c in value.items():
            yearsum += c
            posttotal += c
            bdict[key] = yearsum

    d = defaultdict(list)
    for k, v in adict.items() + bdict.items():
        d[k].append(v)

    return render_template('archive.html', d=d)
jina2


你用的是什么数据库?@SeanVieira,为了开发我用的是sqlite。
# custom filter converts month number to "January", etc.
@app.template_filter('month_number')
def month_name(month_number):
    return calendar.month_name[month_number]

@app.route('/archive')
def display_archive():
    p = db.session.query(Post.pub_date).all()
    d = defaultdict(set) # removes the duplicate months by putting into sets.
    n = {}
    for i in p:
        d[i.pub_date.year].add(i.pub_date.month)
        d.items()

# first part returns eliminates dup months by making sets, needs sorting.
# defaultdict(<type 'set'>, {2012: set([1, 2, 4]),\
# 2013: set([4, 12, 5, 6]), 2014: set([1, 2, 3])})

for key, value in d.iteritems() :
        a = value
        a = list(a) #convert sets to list.
        a.sort()  # soft the list.
        n[key] = a

    return render_template('archive.html', d=d, n=n)

# convert to list for easy sorting.
#{2012: [1, 2, 4], 2013: [4, 5, 6, 12], 2014: [1, 2, 3]}
    <dl>
    {% for key, value in n.iteritems() %}
        <dt>{{ key }}</dt>

        {% for v in value %}
           <dd><a href="/{{ key }}/{{ v }}">{{ v|month_number }}</a></dd>
        {% endfor %}
    {% endfor %}
    </dl>

Again, it works... but not sure how to add the counts.  It will have to be done before the defaultdict function I assume.
@app.route('/archive')
def display_archive():
    p = db.session.query(Post.pub_date).all()
    d = defaultdict(list)
    for i in p:
        d[i.pub_date.year].append(i.pub_date.month)

    # add the monthly counts by counting the instances of month number.
    adict = {}
    for k, v  in d.items():
        adict[k] = Counter(v)

    bdict = {}
    # add the monthly and yearly totals counts
    posttotal = 0
    for key, value in adict.iteritems():
        yearsum = 0
        for m, c in value.items():
            yearsum += c
            posttotal += c
            bdict[key] = yearsum

    d = defaultdict(list)
    for k, v in adict.items() + bdict.items():
        d[k].append(v)

    return render_template('archive.html', d=d)
{% extends "base.html" %}
{% block title %}Archive{% endblock %}
{% block content %}

<dl>
{% for key, value in d.iteritems() %}
    <dt><a href="/{{ key }}">{{ key }}</a> ({{ value[1] }})</dt>
    {% for m, c in value[0].items() %}
       <dd><a href="/{{ key }}/{{ m }}">{{m|month_number}}</a> ({{ c }})</a></dd>
        {{ a }}
    {% endfor %}
{% endfor %}
</dl>


{% endblock %}