django根据日期显示模板

时间:2016-01-25 19:42:30

标签: python django

我想根据系统日期显示某些模板。 让我们说我有模板和模型a,b,c,d。 我会分别向a,b,c,d添加内容,我想让它显示出来 某些模板基于一天中的时间,但在同一个html页面上(称之为alphabet.html)。

Template a on Monday. 
Template b on Tuesday. 
Template c on Wed. 
Template d on Thursday
but URL has to be on same html page. 
The contents/template would be shifting based on the date. 

应该怎么做?应该如何设置视图,模型和网址?

1 个答案:

答案 0 :(得分:2)

这很简单,在你的views.py中你只需if else块:

from django.shortcuts import render

def alphabet_view(request):
    if day == 'Monday':
        return render(request, 'a.html', {..context..})
    elif day == 'Tuesday':
        return render( request, 'b.html', {......})

view_func只有一个网址,但您可以根据逻辑返回您想要的任何模板。

修改

再一次,你应该参考django doc。根据你的评论,你应该像你这样定义你的网址:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^alphabet/$', views.alphabet_view, name='alphabet'),
]