2012-09-03 30 views
1

我写了一个index.html和一个views.py它。我会得到日期并按照下面的说明转换月份数据。它适用于索引页面,但是当我从其他页面扩展索引页面时,日期不会到来。Django - 当我扩展index.html我没有得到变量

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)) 
+1

什么你的意思是“当我从其他页面排除索引页面”? –

+0

我想你的意思是(扩展)或(继承)而不是(排除) –

+0

是的抱歉,我的意思是扩展 – tunaktunak

回答

7

如果你想在此日期和时间中的每个页面,你应该使用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 }} and {{ date_month }}

+0

这也是一个不错的选择! –

+0

谢谢你很酷 – tunaktunak

0

从我的理解。你有一个从另一个延伸的页面。但其他页面不显示继承的数据。

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

当另一个页面继承index.html简单,因为它是由其他功能和处理不views.index()它不更新的发送日期(我希望我的想法是在这里清楚)。

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

+0

我怎样才能做到这一点? – tunaktunak

0

如果我理解你的问题,那么我认为你假设扩展index.html页面还扩展了由def index(request)定义的视图。不幸的是,您必须在每个视图中提供日期变量,因为模板index.html仅提供了一种查看html格式的变量的方法。

而不是在每个视图中重复您的日期格式代码,这看起来像自定义模板过滤器的好例子。查看将上面的索引函数转换为模板标签(请参阅https://docs.djangoproject.com/en/dev/howto/custom-template-tags/),然后您可以在视图中设置日期,例如

def my_other_view(request): 
    context = {'date': my_date } 

,然后在你的模板文件,你可以建立使用my_date_template_filter像这样

{extend 'index.html'} 

{% block content %} 
    {{ my_date|my_date_template_filter}} 
{% endblock %} 

当然,你将不得不在上面的链接定义来定义自定义模板过滤器。