Whatsp
Whatsp

Reputation: 35

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. 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).

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?

Upvotes: 1

Views: 141

Answers (1)

Shang Wang
Shang Wang

Reputation: 25539

That's easy, in your views.py you just do an 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 has only one url, but you could return whichever template you want based on the logic.

Edit:

Once again, you should refer to 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'),
]

Upvotes: 2

Related Questions