0% found this document useful (0 votes)
5 views4 pages

DJango Lab Book - Week2

The document outlines an experiment to develop a Django app that displays the current date and time with a four-hour offset. It includes instructions for creating a virtual environment, project structure, and defining routes in the urls.py file. The app features various views to display time information and provides example URLs for testing the functionality.

Uploaded by

dindukurthysejal
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)
5 views4 pages

DJango Lab Book - Week2

The document outlines an experiment to develop a Django app that displays the current date and time with a four-hour offset. It includes instructions for creating a virtual environment, project structure, and defining routes in the urls.py file. The app features various views to display time information and provides example URLs for testing the functionality.

Uploaded by

dindukurthysejal
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

Week2

(Aim of experiment)
● 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 the server.
● Practice creation of project/application, organizing its folder structure
● Familiarize with application and URLConfs(routes)
● Create new routes and define new views

Procedure:
Repeat the Week1 experiment
Create venv, project and app.
Design new routes in the [Link] file to invoke additional methods.

Project

[Link]
from [Link] import admin
from [Link] import include, path

urlpatterns = [
path('admin/', [Link]),
path('', include('[Link]')),
]

App

[Link]
from [Link] import path, re_path
from . import views

urlpatterns = [
path('', [Link], name='hello'),
path('time/',[Link], name='welcome'),
path('time/plusminus4',views.plusminus4, name='Now plus minus 4'),
re_path(r'^time/plus/(\d{1,2})$',[Link], name='hours
ahead'),
]
App
[Link]

from [Link] import render


from [Link] import HttpResponse
import datetime
# Create your views here.
def hello(request):
return HttpResponse("Hello World!")

def welcometime(request):
now = [Link]()
html = "<html><body>It is now %s</body></html>"% now
return HttpResponse("Welcome!"+ html)

def hoursahead(request, offset):


offset = int(offset)
dt = [Link]() + [Link](hours=offset)
html = "In %s hour(s), it will be %s." % (offset, dt)
return HttpResponse(html)

def plusminus4(request):
offset = 4
dtplus4 = [Link]() + [Link](hours=offset)
dtminus4 = [Link]() - [Link](hours=offset)
html = "%s hour(s) back, it was %s.<br> In %s hour(s), it will be %s."
% (offset, dtminus4, offset, dtplus4)
return HttpResponse(html)

Urls to try

1. [Link]:8000/
2. [Link]:8000/time

3. [Link]:8000/time/plusminus4

4. [Link]:8000/time/plus/99

5. [Link]:8000/time/plus/100
6. See URL(example of input validations via regexp)

~*~

You might also like