0% found this document useful (0 votes)
11 views

Experiment-04 Full Stack Django Search Creators

Uploaded by

amplifydocument
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Experiment-04 Full Stack Django Search Creators

Uploaded by

amplifydocument
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

21CS62 | Full Stack Django Development|

Experiment-04

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.

Step-01: This app will be created in the Django project we made earlier.

Step-02: Add the app to INSTALLED_APPS

Open the settings.py file in your project's directory (e.g., myproject/settings.py).

Locate the INSTALLED_APPS list and add the name of your new app to the list:

Search Creators…. Page 31


21CS62 | Full Stack Django Development|

Step-03: Create a view function

• Open the views.py file in your Django app's directory


(e.g., fourdate_timeapp/views.py).
• Import the necessary modules at the top of the file
• Create a new view function that will handle the request and render the date and time:

from django.shortcuts import render

import datetime

from dateutil import tz

def datetime_offsets(request):

now = datetime.datetime.now()

context = {

'current_datetime': now,

'four_hours_ahead': now + datetime.timedelta(hours=4),

'four_hours_before': now - datetime.timedelta(hours=4),

return render(request, 'datetime_offsets.html', context)

• This view function gets the current date and time using datetime.datetime.now(),
calculates the date and time four hours ahead and four hours before using
datetime.timedelta, and then passes all three values to the template as context variables.

Search Creators…. Page 32


21CS62 | Full Stack Django Development|

Step-04: Create a new template

• In your app's directory (e.g., fourdate_timeapp), create a new folder named


templates (if it doesn't already exist).
• Inside the templates folder, create another folder with the same name as your app
(e.g., fourdate_timeapp).
• Inside the fourdate_timeapp folder, create a new file named
datetime_offsets.html.
• Open datetime_offsets.html and add the following code to display the current
date and time, along with the offsets:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

Search Creators…. Page 33


21CS62 | Full Stack Django Development|

<title>Date and Time Offsets</title>

<!-- Bootstrap CSS -->

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css"
rel="stylesheet">

<style>

/* Center content vertically */

html, body {

height: 100%;

display: flex;

justify-content: center;

align-items: center;

</style>

</head>

<body>

<div class="container text-center">

<h1>Current Date and Time on the Server</h1>

<p>{{ current_datetime }}</p>

<h2>Four Hours Ahead</h2>

<p>{{ four_hours_ahead }}</p>

<h2>Four Hours Before</h2>

Search Creators…. Page 34


21CS62 | Full Stack Django Development|

<p>{{ four_hours_before }}</p>

</div>

<!-- Bootstrap Bundle with Popper -->

<script
src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-
alpha1/dist/js/bootstrap.min.js"></script>

</body>

</html>

• This template displays the current_datetime, four_hours_ahead, and four_hours_before


variables passed from the view function.

Search Creators…. Page 35


21CS62 | Full Stack Django Development|

Step-05: Map the view function to a URL

• Open the urls.py file in your Django app's directory (e.g., fourdate_timeapp/urls.py).
• Import the view function at the top of the file
• Add a new URL pattern to the urlpatterns list

from django.urls import path

from . import views

urlpatterns = [

path('datetime-offsets/', views.datetime_offsets, name='datetime_offsets'),

Search Creators…. Page 36


21CS62 | Full Stack Django Development|

Step-06: Include the app's URLs in the project's URL patterns

• Open the urls.py file in your project's directory (e.g., fullstack_project/urls.py).


• Import the include function from django. urls and the path function from django. urls:

from django.urls import include, path

• Add a new URL pattern to the urlpatterns list

path('', include('fourdate_timeapp.urls')),

• This includes the URL patterns from your app's urls.py file.

Search Creators…. Page 37


21CS62 | Full Stack Django Development|

Step-07: Run the development server

• In the VS Code terminal, navigate to your Django project's directory (the one containing
manage.py).
• Run the development server

python manage.py runserver

• Open your web browser and visit http://127.0.0.1:8000/.


• Type or copy this http://127.0.0.1:8000/datetime-offsets/

Final Output of the Date and Time App

Search Creators…. Page 38

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy