0

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

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

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

4

1 回答 1

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'
>>>
于 2012-12-13T09:55:10.563 回答