Edit urls.py file

  • In the same previous project add the offsets url for printing the date time with offset of current date and time.

from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path("datetime/",views.datetimee,name="datetime"),
    path("offset/",views.offset,name="offset"),
]
        

Edit views.py

  • views.py should be created here datetime/datetime/views.py, and add the below contents to it.

from django.shortcuts import HttpResponse,render
from datetime import datetime,timedelta

def datetimee(request):
    now = datetime.now()
    return HttpResponse(f"current datetime is {now}") 

def offset(request):
    now = datetime.now()
    offset=4
    four_before=now-timedelta(hours=offset)
    four_after=now+timedelta(hours=offset)
    HTML=f"<html><head><title>something</title></head>"\
        f"<p>current time is: {now}"\
        f"<p>four houres after is: {four_after}"\
        f"<p>four houres before is: {four_before}"
    return HttpResponse(HTML)
        

See the OUTPUT

  • After saving the written program files run the local server with python manage.py runserver command and follow the localhost link.
  • redirect the local host line to the written url datetime/
  • the current datetime should be printed as output.