django book example HELP..

263 views
Skip to first unread message

Dipo Elegbede

unread,
Feb 18, 2011, 10:04:13 AM2/18/11
to django-users
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
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

Tom Evans

unread,
Feb 18, 2011, 10:20:07 AM2/18/11
to django...@googlegroups.com
On Fri, Feb 18, 2011 at 3:04 PM, Dipo Elegbede <dele...@dudupay.com> wrote:
>
> 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>
> 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.
>
>
>

These should help you:

http://docs.djangoproject.com/en/1.2/topics/http/urls/#named-groups
http://docs.djangoproject.com/en/1.2/topics/http/urls/#notes-on-capturing-text-in-urls

Also, please configure your MUA to wrap lines at ~78 characters when
sending emails to mailing lists.

Cheers

Tom

Dipo Elegbede

unread,
Feb 18, 2011, 10:31:17 AM2/18/11
to django...@googlegroups.com
Thumbs up Tom.

I didn't even bother to open the links you sent, I just finished the tutorial.
The name of the link said it all, capturing-text-in urls.

I am most grateful.

thanks all.

I would have to read the links however to master these things.

Regards.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Chris Cogdon

unread,
Dec 13, 2012, 5:49:44 PM12/13/12
to django...@googlegroups.com, dele...@dudupay.com
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写道:
Reply all
Reply to author
Forward
0 new messages