Django项目2
1.准备工作
[plain]
django-admin.py startproject web01
django-admin.py startapp tim
python manage.py runserver
2.settings注册
[python]
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'tim',
)
3.配置url
[python]
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
url(r'^tim/current/$','tim.views.current'), #映射views.py里的current
url(r'^tim/plus/(\d{1,2})/$','tim.views.ahead'),#这里的数字部分,转化为元组,方便被offset取值
)
4.views视图文件,这里不用导入外部的html文件,所以不需要loader和Context了
[python]
#coding:utf8
from django.http import HttpResponse
import datetime
def current(req):
now = datetime.datetime.now()
html='现在时间是 %s' % now
return HttpResponse(html)
def ahead(req,offset): #这里的offset只是从url得到的传入值,变量名其实可以任意
try:
offset = int(offset)
except ValueError:
raise Http404()
dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
html = '%s 小时后,时间将变为 %s' % (offset,dt) #经过offset小时后,时间变为dt
return HttpResponse(html)
补充:Web开发 , Python ,