Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在django中创建和渲染年和月结构_Python_Django_Django Templates_Python Datetime - Fatal编程技术网

Python 在django中创建和渲染年和月结构

Python 在django中创建和渲染年和月结构,python,django,django-templates,python-datetime,Python,Django,Django Templates,Python Datetime,在我的博客应用程序中,我需要一个结构(作为上下文处理器中的变量创建),该结构将存储月份数和到当前月份为止的连续5个月的相应年份。所以如果当前月份是12月,我们将有年份:2010和月份:12,11,10,9,8。如果月份是一月,我们将有2010年:月份:1和年份:2009月份:12、11、10、9。我的目标是以以下形式显示存档: - 2010 - January - 2009 - December - November - October - Septemb

在我的博客应用程序中,我需要一个结构(作为上下文处理器中的变量创建),该结构将存储月份数和到当前月份为止的连续5个月的相应年份。所以如果当前月份是12月,我们将有年份:2010和月份:12,11,10,9,8。如果月份是一月,我们将有2010年:月份:1和年份:2009月份:12、11、10、9。我的目标是以以下形式显示存档:

- 2010
    - January
- 2009
    - December
    - November
    - October
    - September
如何创建它,我应该使用什么结构?那么如何展示呢?我想我需要一些嵌套结构,但可以用django<1.2?
我自己开始的,但在某个时候完全迷失了方向:

now = datetime.datetime.now()

years = []
months = []
archive = []
if now.month in range(5, 12, 1):
    months = range(now.month, now.month-5, -1)        
    if months:
        years = now.year
else:
    diff = 5 - now.month
    for i in range(1, now.month, 1):
        archive.append({
                        "month": i,
                        "year": now.year,
        })

    for i in range(0, diff, 1):
        tmpMonth = 12 - int(i)
        archive.append({
                        "month": tmpMonth,
                        "year": now.year-1,
        })

    if archive:
        years = [now.year, now.year-1]
如何创建它,我应该使用什么结构

我会用一个年-月元组列表。下面是一个示例实现。你需要一个方便的图书馆来完成这项工作

from datetime import datetime
from dateutil.relativedelta import relativedelta

def get_5_previous_year_months(a_day):
    """Returns a list of year, month tuples for the current and previous 
    5 months relative to a_day"""
    current_year, current_month = a_day.year, a_day.month
    first_of_month = datetime(current_year, current_month, 1)
    previous_months = (first_of_month - relativedelta(months = months)
            for months in range(0, 5))
    return ((pm.year, pm.month) for pm in previous_months) 

def get_current_and_5_previous_months():
    return get_5_previous_year_months(datetime.today())
那么如何展示呢

这里是一个非常简单的方式来显示它。我认为可以通过将
元素替换为
并对其进行适当的样式设置来清理它

    <ul>
    {% for year, month in previous_year_months %}
        {% ifchanged year %}
            </ul><li>{{ year }}</li><ul>
        {% endifchanged %}
                <li>{{ month }}</li>
    {% endfor %}
    </ul>
    {年百分比,前一年的月百分比} {%ifchanged year%}
    • {{year}
      • {%endifchanged%}
      • {{month}
      • {%endfor%}
其中,
previous\u year\u months
是一个上下文变量,对应于
get\u current\u和\u 5\u previous\u months
返回的结果