简体   繁体   中英

URL mapping in Python/Django

I just started to learn Django and Python.

I use the online book from djangobook.com

In chapter 3, ( http://djangobook.com/en/1.0/chapter03/ ) I am trying out the sample to add x hours to current time. My files below:

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)

But if I try to navigate to: http://127.0.0.1:8000/time/plus/5/ , I get a NameError at /time/plus/5/ . Am I missing something?

Thanks.

EDIT

Dump here - http://pastebin.com/Hn3aFLzR

您忘记在urls.py中导入hours_ahead了:

from mysite.views import current_datetime, hours_ahead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM