Views and URLconfs(The Definitive Guild to Django)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 from django.http import HttpResponse, Http404
2 import datetime
3
4 def hello(request):
5 return HttpResponse("Hello world")
6
7 def abc(request):
8 now = datetime.datetime.now()
9 html = "<html><body>It is now %s.</body></html>" % now
10 return HttpResponse(html)
11
12 def hours_ahead(request, offset):
13 try:
14 offset = int(offset)
15 except ValueError:
16 raise Http404()
17 dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
18 html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
19 return HttpResponse(html)
2 import datetime
3
4 def hello(request):
5 return HttpResponse("Hello world")
6
7 def abc(request):
8 now = datetime.datetime.now()
9 html = "<html><body>It is now %s.</body></html>" % now
10 return HttpResponse(html)
11
12 def hours_ahead(request, offset):
13 try:
14 offset = int(offset)
15 except ValueError:
16 raise Http404()
17 dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
18 html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
19 return HttpResponse(html)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from django.conf.urls.defaults import *
2 from mysite.views import hello, abc, hours_ahead
3
4 # Uncomment the next two lines to enable the admin:
5 # from django.contrib import admin
6 # admin.autodiscover()
7
8 urlpatterns = patterns('',
9 ('^hello/$', hello),
10 ('^time/$', abc),
11 (r'^time/plus/(\d{1,2})/$', hours_ahead),
12 # Example:
13 # (r'^mysite/', include('mysite.foo.urls')),
14
15 # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
16 # to INSTALLED_APPS to enable admin documentation:
17 # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
18
19 # Uncomment the next line to enable the admin:
20 # (r'^admin/', include(admin.site.urls)),
21 )
2 from mysite.views import hello, abc, hours_ahead
3
4 # Uncomment the next two lines to enable the admin:
5 # from django.contrib import admin
6 # admin.autodiscover()
7
8 urlpatterns = patterns('',
9 ('^hello/$', hello),
10 ('^time/$', abc),
11 (r'^time/plus/(\d{1,2})/$', hours_ahead),
12 # Example:
13 # (r'^mysite/', include('mysite.foo.urls')),
14
15 # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
16 # to INSTALLED_APPS to enable admin documentation:
17 # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
18
19 # Uncomment the next line to enable the admin:
20 # (r'^admin/', include(admin.site.urls)),
21 )