我为它写了一个 index.html 和一个 views.py。我得到日期并将月份数据转换为口头数据,如下所示。它适用于索引页面,但是当我从其他页面扩展索引页面时,日期没有出现。
def index(request):
datenow = date.today()
datemonth = date.today().month
if datemonth == 8:
date_month="Ağustos"
elif datemonth == 9:
date_month = "Eylül"
elif datemonth == 10:
date_month = "Ekim"
elif datemonth == 11:
date_month ="Kasım"
elif datemonth == 12:
date_month ="Aralık"
elif datemonth == 1:
date_month ="Ocak"
elif datemonth == 2:
date_month ="Şubat"
elif datemonth == 3:
date_month ="Mart"
elif datemonth == 4:
date_month ="Nisan"
elif datemonth == 5:
date_month ="Mayıs"
elif datemonth == 6:
date_month ="Haziran"
elif datemonth == 7:
date_month ="Temmuz"
news = New.objects.all()[:10]
programs= Program.objects.filter(date=date.today())
print date.today()
print date_month
template = "index.html"
context = {'news':news,
'programs':programs,
'datenow':datenow,
'date_month':date_month}
return render_to_response(template,context,context_instance=RequestContext(request))
请您参考如下方法:
如果你想在每个页面中都有这个日期和时间,你应该使用 Django 上下文处理器 Link Here
def datetime(request):
datenow = date.today()
datemonth = date.today().month
if datemonth == 8:
date_month="Ağustos"
elif datemonth == 9:
date_month = "Eylül"
elif datemonth == 10:
date_month = "Ekim"
elif datemonth == 11:
date_month ="Kasım"
elif datemonth == 12:
date_month ="Aralık"
elif datemonth == 1:
date_month ="Ocak"
elif datemonth == 2:
date_month ="Şubat"
elif datemonth == 3:
date_month ="Mart"
elif datemonth == 4:
date_month ="Nisan"
elif datemonth == 5:
date_month ="Mayıs"
elif datemonth == 6:
date_month ="Haziran"
elif datemonth == 7:
date_month ="Temmuz"
context = {'datenow':datenow,'date_month':date_month}
return context
在 settings.py 中
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.csrf',
# Custom Context Proccessors
'apps.your-app.context_processor.datetime',
)
然后在 HTML 文件中,你可以使用
{{ datenow }} 和 {{ date_month }}
