I’m new in django and I’m reading a tutorial and it have an example but I think is a old Django version and now I’m using Django 1.4
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)
and the urls.py file
from django.conf.urls.defaults import *
from mysite.views import current_datetime, hours_ahead
urlpatterns = patterns('',
(r'^time/$', current_datetime),
(r'^time/plus/\d{1,2}/$', hours_ahead),
)
Your URL pattern is missing parenthesis to show that you want to capture the offset from the URL. Try changing it to the following:
Often, people prefer to use named groups in their URL patterns. In your case, the URL pattern would change to: