Develop a Django app that displays date and time four hours ahead and four hours before as an offset of current date and time in server

Develop a Django app that displays date and time four hours ahead and four hours before as an offset of current date and time in server

Program:-

Views.py:-

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body><h1>It is now %s.</h1></body></html>" % now
    return HttpResponse(html)

def four_hours_ahead(request):
    dt = datetime.datetime.now() + datetime.timedelta(hours=4)
    html = "<html><body><h1>After 4 hour(s), it will be %s.</h1></body></html>" % (dt,)
    return HttpResponse(html)

def four_hours_before(request):
    dt = datetime.datetime.now() + datetime.timedelta(hours=-4)
    html = "<html><body><h1>Before 4 hour(s), it was %s.</h1></body></html>" % (dt,)
    return HttpResponse(html)

urls.py:-

# In project named first, make following changes to urls.py
from django.contrib import admin
from django.urls import path
from lab11.views import current_datetime, four_hours_ahead, four_hours_before

urlpatterns = [
    path('cdt/', current_datetime),
    path('fhrsa/', four_hours_ahead),
    path('fhrsb/', four_hours_before),
]

Leave a Reply

Your email address will not be published. Required fields are marked *