[python]django学习笔记 二
视图Helloworld
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
def hello(request):
return HttpResponse("Hello world")
路由文件
urls.py
from django.conf.urls.defaults import*
from mysite.views import hello
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# (r'^admin/', include(admin.site.urls)),
('^hello/$', hello),
)
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# (r'^admin/', include(admin.site.urls)),
('^hello/$', hello),
)
动态路由
urlpatterns = patterns('',
# ...
(r'^time/plus/\d+/$', hours_ahead),
# ...
)
# ...
(r'^time/plus/\d+/$', hours_ahead),
# ...
)
views.py
from django.http import Http404, HttpResponse
import datetime
def hours_ahead(request, offset):
try:
offset = int(offset)
except ValueError:
raise Http404()
dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
html ="<html><body>In %s hour(s), it will be %s.</body></html>"% (offset, dt)
return HttpResponse(html)
import datetime
def hours_ahead(request, offset):
try:
offset = int(offset)
except ValueError:
raise Http404()
dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
html ="<html><body>In %s hour(s), it will be %s.</body></html>"% (offset, dt)
return HttpResponse(html)
think in coding