Django - 当我扩展index.html时,我没有得到变量

tun*_*nak 1 python django django-templates django-views

我为它写了一个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))
Run Code Online (Sandbox Code Playgroud)

Jub*_*mas 7

如果您想在每个页面中使用此日期和时间,则必须使用Django上下文处理器链接此处

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
Run Code Online (Sandbox Code Playgroud)

在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',


)
Run Code Online (Sandbox Code Playgroud)

然后在HTML文件中,您可以使用

{{ datenow }} 和 {{ date_month }}