Python Django-当我扩展index.html时,我不会得到变量

Python Django-当我扩展index.html时,我不会得到变量,python,django,django-templates,django-views,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:

我为此编写了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))

据我所知。您有一个从另一个页面扩展而来的页面。但另一个页面不显示继承的数据

这只是因为index.html是从views.py调用的。变量和更新从view.index发送

当另一个页面继承index.html时,它不会更新日期,因为它是由另一个函数处理的,而不是views.index。我希望我的想法在这里很清楚


解决这个问题的一个简单方法是复制views.index的内容,并将变量再次发送到新的html模板。

据我所知。您有一个从另一个页面扩展而来的页面。但另一个页面不显示继承的数据

这只是因为index.html是从views.py调用的。变量和更新从view.index发送

当另一个页面继承index.html时,它不会更新日期,因为它是由另一个函数处理的,而不是views.index。我希望我的想法在这里很清楚


解决此问题的一个简单方法是复制views.index的内容,并将变量再次发送到新的html模板。

如果我理解您的问题,那么我认为您假设扩展index.html页面也会扩展def indexrequest定义的视图。不幸的是,您必须在每个视图中提供日期变量,因为template index.html只提供了以html格式查看变量的方法

与在每个视图中重复日期格式代码不同,这似乎是自定义模板过滤器的好例子。看看如何将上面的索引函数转换为templatetag,然后可以在视图中设置日期,例如

def my_other_view(request):
    context = {'date': my_date }
然后在你的模板文件中,你可以像这样使用my_date_template_filter构建


当然,您必须按照上面链接中的定义定义自定义模板过滤器

如果我理解您的问题,那么我认为您是在假设扩展index.html页面也会扩展def indexrequest定义的视图。不幸的是,您必须在每个视图中提供日期变量,因为template index.html只提供了以html格式查看变量的方法

与在每个视图中重复日期格式代码不同,这似乎是自定义模板过滤器的好例子。看看如何将上面的索引函数转换为templatetag,然后可以在视图中设置日期,例如

def my_other_view(request):
    context = {'date': my_date }
然后在你的模板文件中,你可以像这样使用my_date_template_filter构建


当然,您必须按照上面链接中的定义定义自定义模板过滤器

如果要在每个页面中显示此日期和时间,则必须使用Django上下文处理器

在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}}

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

在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}}

当我从其他页面中排除索引页时,你是什么意思?我猜你的意思是扩展或继承而不是排除是的,对不起,我的意思是扩展理解,你创建的index.html和你创建的index函数之间没有直接链接。html是模板,其本身不调用index函数。必须通过“上下文”字典向模板提供变量,如上所述,每次都使用模板。当我从其他页面中排除索引页面时,你是什么意思?我猜你的意思是扩展或继承而不是排除。是的,对不起,我的意思是扩展理解您创建的index.html和您创建的index函数之间没有直接链接。html是模板,其本身不调用index函数。每次使用模板时,必须通过“上下文”字典向模板提供变量,如上所述。我如何为flatpages提供变量?我如何为flatpages提供变量?