Reputation: 22983
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
Upvotes: 1
Views: 360
Reputation: 70031
You forget to import hours_ahead
in urls.py:
from mysite.views import current_datetime, hours_ahead
Upvotes: 5