[python]django學習筆記 二

weixin_34262482發表於2010-12-31

檢視Helloworld

 

from django.http import HttpResponse

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),
)

 

動態路由

urlpatterns = patterns('',
# ...
(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)

 

 

相關文章