2011-04-15 53 views
1

我剛開始學習Django和Python。Python中的URL映射/ Django

我使用djangobook.com

在線圖書在第三章(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

回答

5

你忘了在urls.py進口hours_ahead

from mysite.views import current_datetime, hours_ahead 
+0

Aagghh,是的,是我不好..謝謝:d – bdhar 2011-04-15 08:56:23