【问题标题】:convert month numeric form into their name?将月份数字形式转换为他们的名字?
【发布时间】:2012-11-30 23:25:21
【问题描述】:

我使用此代码打印月份。 在 views.py

currentMonth = datetime.now().month return render_to_response('showroom.html',{'currentMonth':currentMonth,} , context_instance=RequestContext(request))

然后打印我们文件中的变量{{currentMonth}}显示 12,但我想显示 12 月。

【问题讨论】:

标签: python templates django-templates


【解决方案1】:

你想要这样的东西:

# Near the top of views.py
from datetime import datetime

# ...
currentMonth = datetime.now().strftime('%B')
return render_to_response('showroom.html',{'currentMonth':currentMonth,} , context_instance=RequestContext(request))

在您上面评论中的代码中:

import datetime
currentMonth = datetime.now().month
currentMonthn = currentMonth.strftime("%B")

currentMonth 是一个整数,没有strftime 方法,但datetime.now() 返回一个对象:

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.now().strftime('%B')
'December'
>>>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2011-06-04
    相关资源
    最近更新 更多