Typo on that typo! That slash should also be inside the parenthesis:
(r'^time/plus/(\d{1,2})/$', hours_ahead)
Note, though, I tend not to be so specific with URL matching... consider if
someone hacked a URL to read time/plus/999/ ... The above will cause a 404,
where letting it through to your code, with something like (\d+) gives you
the opportunity to respond instead with something more specific to the
error, such as redirecting to a "sensible default" along with a message.
The "correct behavior" depends on your application design.
On Thursday, December 13, 2012 1:32:30 AM UTC-8, 向浩 wrote:
>
> (r'^time/plus/\d{1,2}/$', hours_ahead),this line,you should use () for
> d{1,2}
> (r'^time/plus/\(d{1,2})/$', hours_ahead)
>
> 在 2011年2月18日星期五UTC+8下午11时04分13秒,Dipo Elegbede写道:
>>
>> Hi all,
>>
>> i am currently reading the django book and following the examples step by
>> step.
>>
>> I have a view defined as follows:
>>
>> from django.http import Http404, HttpResponse
>> import datetime
>>
>> #def myhome(request):
>> # message = """<html>
>> # <head><title>MY HOME</title></head>
>> # <body bgcolor='yellow'>
>> # <center>
>> # <h1><span style='color:green'>This is my
>> </span><span style='color:white'>way of</span><span style='color:green'>
>> saying welcome!</span></h1>
>> # </center>
>> # </body>
>> # </html>
>> # """
>> # return HttpResponse(message)
>> #
>> #def hello(request):
>> # return HttpResponse("Hello World")
>> #
>> #def current_time(request):
>> # now = datetime.datetime.now()
>> # html = "<html><body>It is now %s.</body></html>" % now
>> # return HttpResponse(html)
>>
>> def hours_ahead(request, offset):
>> try:
>> offset = int(offset)
>> except ValueError:
>> raise Http404()
>> 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)
>>
>> (All the commented area work just fine )
>>
>> and I have a url.py like this:
>>
>> from django.conf.urls.defaults import *
>> from mysite.views import *
>> from django.contrib import admin
>> admin.autodiscover()
>>
>> urlpatterns = patterns('',
>> (r'^$', myhome),
>> (r'^polls/', include('mysite.polls.urls')),
>> (r'^hello/$', hello),
>> (r'^time/$', current_time),
>> (r'^time/plus/\d{1,2}/$', hours_ahead),
>> )
>>
>> when i try to run localhost:8000/time/plus/3, I get the following error:
>>
>> TypeError at /time/plus/4/
>> hours_ahead() takes exactly 2 arguments (1 given)
>> Request Method: GET
>> Request URL: http://localhost:8000/time/plus/4/
>> Django Version: 1.2.1
>> Exception Type: TypeError
>> Exception Value:
>> hours_ahead() takes exactly 2 arguments (1 given)
>> Exception Location:
>> c:\Python26\lib\site-packages\django-1.2.1-py2.6.egg\django\core\handlers\base.py
>>
>> in get_response, line 100
>> Python Executable: c:\Python26\python.exe
>> Python Version: 2.6.4
>> Python Path: ['c:\\users\\owner\\desktop\\djtask\\mysite',
>> 'c:\\Python26\\lib\\site-packages\\django-1.2.1-py2.6.egg',
>> 'c:\\Python26\\lib\\site-packages\\pip-0.8.2-py2.6.egg',
>> 'C:\\Windows\\system32\\python26.zip', 'c:\\Python26\\DLLs',
>> 'c:\\Python26\\lib', 'c:\\Python26\\lib\\plat-win',
>> 'c:\\Python26\\lib\\lib-tk', 'c:\\Python26',
>> 'c:\\Python26\\lib\\site-packages',
>> 'c:\\Python26\\lib\\site-packages\\PIL',
>> 'c:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']
>> Server time: Fri, 18 Feb 2011 15:51:57 +0100
>>
>> My understanding of the error message is that I supplied, 1 argument when
>> the view function, hours_ahead was expecting 2 but really, i don't have a
>> hang of where to put the other argument. I am following the examples in the
>> book.
>>
>> I must be missing something, kindly help me out.
>>
>> thank you.
>>
>>
>>
>>
>>
>>
>>
>> --
>> Elegbede Muhammed Oladipupo
>> OCA
>> +2348077682428
>> +2347042171716
>> www.dudupay.com
>> Mobile Banking Solutions | Transaction Processing | Enterprise
>> Application Development
>>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/u9IUohV8fWsJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.