我刚开始学习Django和Python。
中的在线图书在第3章(http://djangobook.com/en/1.0/chapter03/)中,我正在尝试将x小时添加到当前时间。我的文件如下:
urls.py
from django.conf.urls.defaults import patterns, include, url
from mysite.views import current_datetime
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
(r'^time/$', current_datetime),
(r'^time/plus/(\d{1,2})/$', hours_ahead),
)
views.py
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
def hours_ahead(request, offset):
offset = int(offset)
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)
但如果我尝试导航到http://127.0.0.1:8000/time/plus/5/,我会得到NameError at /time/plus/5/
。我错过了什么吗?
感谢。
修改
转到此处 - http://pastebin.com/Hn3aFLzR
答案 0 :(得分:5)
您忘记在urls.py中导入hours_ahead
:
from mysite.views import current_datetime, hours_ahead