[英]URL mapping in Python/Django
我刚刚开始学习Django和Python。
我使用djangobook.com上的在线图书
在第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 : NameError at /time/plus/5/
,则会NameError at /time/plus/5/
收到NameError at /time/plus/5/
。 我想念什么吗?
谢谢。
编辑
在这里转储-http: //pastebin.com/Hn3aFLzR
您忘记在urls.py中导入hours_ahead
了:
from mysite.views import current_datetime, hours_ahead
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.