Module 1
Additional Programs for Practice
1) Program to display default UTC time zone in Django
[Link]
from [Link] import render
from [Link] import timezone
import datetime
def hours(request, offset):
now = [Link]()
dt = now + [Link](hours=offset)
dt1 = now - [Link](hours=offset)
context = {
'current_time': now,
'offset': offset,
'future_time': dt,
'before_time': dt1,
}
return render(request, '[Link]', context)
Note: All the other codes are same as of current date time program
2) Program to display the particular webpage only for the authorized users not
other users.
Note: This program needs the user credentials created using python [Link]
createsuperuser command
[Link]
from [Link] import render
from [Link] import HttpResponseForbidden
def special_page(request):
if [Link].is_authenticated and [Link] == "superuser":
return render(request, "[Link]")
else:
return HttpResponseForbidden("You do not have permission to view this
page.")
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Special Page</title>
</head>
<body>
<h1>Welcome Sandhya! You have access to this page.</h1>
</body>
</html>
3) Program to display an image by placing the image in the static folder.
Note: The folder named as static has to be created inside the app folder. The
static folder can contain the image file.
[Link]
from [Link] import render
def home(request):
return render(request, '[Link]')
[Link]
<!DOCTYPE html>
<html><head>
<title>Django Static Image
Demo</title>
{% load static %}
<!--this imports static tag library which give tools to access the static files-->
</head><body>
<h1>Displaying an Image from Static Files</h1>
<img src="{% static '[Link]' %}" alt="Demo Image" width="900"
height="600">
</body></html>