Start Django-app

  • Create a new django project django-admin startproject timedate.
  • change into that directory cd timedate.
  • Check if everything is working properly by running local server and seeing the output python manage.py runserver.

Edit urls.py file

  • Set URL patterns to display the date and time.

from django.contrib import admin
from django.urls import path
from . import views

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

Create views.py and Edit it

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

from django.shortcuts import HttpResponse
from datetime import datetime

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

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 local host url to this pattern http://localhost:8000/datetime/, if you are running local host on port 8000
  • the current datetime should be printed as output.

Watch a quick video summary on how to run this program

Watch it at 2x speed