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)
~*~