0% found this document useful (0 votes)
3 views2 pages

Module 1 - Additional Programs For Practice

The document provides three Django programs for practice: one to display the default UTC time zone, another to restrict access to a webpage for authorized users, and a third to display an image from a static folder. Each program includes relevant code snippets for the views and HTML templates. Additionally, it mentions the need for user credentials for the second program and the creation of a static folder for the third program.

Uploaded by

ibaadrahman64
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)
3 views2 pages

Module 1 - Additional Programs For Practice

The document provides three Django programs for practice: one to display the default UTC time zone, another to restrict access to a webpage for authorized users, and a third to display an image from a static folder. Each program includes relevant code snippets for the views and HTML templates. Additionally, it mentions the need for user credentials for the second program and the creation of a static folder for the third program.

Uploaded by

ibaadrahman64
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

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>

You might also like