简体   繁体   English

django根据日期显示模板

[英]django show template based on date

I want to show certain template based on system date. 我想根据系统日期显示某些模板。 Let's say I have templates and models a, b, c, d. 假设我有模板和模型a,b,c,d。 I would be adding content to a, b, c, d separately, and I want it to show certain template based on time of the day, but on the same html page(call it alphabet.html). 我将分别添加内容到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. 

How should this be done? 应该怎么做? How should views, models, urls be set up? 视图,模型,URL应该如何设置?

That's easy, in your views.py you just do an if else block: 这很容易,在您的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 has only one url, but you could return whichever template you want based on the logic. view_func只有一个URL,但是您可以根据逻辑返回所需的任何模板。

Edit: 编辑:

Once again, you should refer to django doc. 再一次,您应该参考django doc。 According to your comment, you should define you url like so: 根据您的评论,您应该这样定义网址:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM