我想根据系统日期显示某些模板。 假设我有模板和模型 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.
这应该如何完成? View 、模型、url 应该如何设置?
最佳答案
这很简单,在你的views.py中你只需做一个if else
block :
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
只有一个 url,但您可以根据逻辑返回您想要的任何模板。
编辑:
再次,您应该引用 django 文档。根据您的评论,您应该像这样定义您的网址:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^alphabet/$', views.alphabet_view, name='alphabet'),
]
关于python - django 基于日期显示模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35000947/