Experiment-04 Full Stack Django Search Creators
Experiment-04 Full Stack Django Search Creators
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.
Locate the INSTALLED_APPS list and add the name of your new app to the list:
import datetime
def datetime_offsets(request):
now = datetime.datetime.now()
context = {
'current_datetime': now,
• 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.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css"
rel="stylesheet">
<style>
html, body {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
</style>
</head>
<body>
</div>
<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>
• 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
urlpatterns = [
path('', include('fourdate_timeapp.urls')),
• This includes the URL patterns from your app's urls.py file.
• In the VS Code terminal, navigate to your Django project's directory (the one containing
manage.py).
• Run the development server