[英]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.