TypeError: 'tuple' object is not callable

1,148 views
Skip to first unread message

kalyani ram

unread,
Feb 20, 2012, 3:01:21 AM2/20/12
to Django users
Hey all,

I am recently trying out the examples from the link
http://www.djangobook.com/en/1.0/chapter03/, where the current time
have to be displayed and then modify and display the offset. I was
able to display the time without any issue, but with the offset part,
i get the error
TypeError at /time/plus/1
'tuple' object is not callable
Exception location: mysite\urls.py in <module>, line 6

My urls.py is as follows.

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),
)

my views.py is as follows.

from django.shortcuts import render_to_response
import datetime

def current_datetime(request):
now = datetime.datetime.now()
return render_to_response('temp.html', {'current_date': now})

def hours_ahead(request, offset):
offset = int(offset)
dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
return render_to_response('temp.html', {'hour_offset': offset},
{'next_time': dt })

my temp.html is as follows:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>Future time</title>
</head>
<body>
<h1>My helpful timestamp site</h1>
<p>In {{ hour_offset }} hour(s), it will be {{ next_time }}.</p>

<hr>
<p>Thanks for visiting my site.</p>
</body>
</html>


pls help. I am really lost :(

akaariai

unread,
Feb 20, 2012, 3:49:34 AM2/20/12
to Django users
On Feb 20, 10:01 am, kalyani ram <arch.kalu3...@gmail.com> wrote:
> My urls.py is as follows.
>
> 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),
> )

Missing comma after the first pattern. Thus you have (r'^time/$',
current_datetime)(...), which is syntax for calling the first tuple.
That causes the error.

> def hours_ahead(request, offset):
>     offset = int(offset)
>     dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
>     return render_to_response('temp.html', {'hour_offset': offset},
> {'next_time': dt })

If I am not mistaken you should not have two dictionaries on the last
line, just one:
{'hour_offset': offset, 'next_time': dt }

- Anssi

kalyani ram

unread,
Feb 20, 2012, 4:16:53 AM2/20/12
to Django users
Thank you very much Anssi. I just got it perfect :D I know i am so
silly with errors. but i am beginner. Thanks alot.
Can u help me one more error?
I trying to make a db connection using postgresql and python2.7
when i type import postgresql and enter I get the following error:

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import postgresql

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import postgresql
ImportError: No module named postgresql
>>>
Help me plz

akaariai

unread,
Feb 20, 2012, 4:32:39 AM2/20/12
to Django users
On Feb 20, 11:16 am, kalyani ram <arch.kalu3...@gmail.com> wrote:
> Thank you very much Anssi. I just got it perfect :D I know i am so
> silly with errors. but i am  beginner. Thanks alot.
> Can u help me one more error?
> I trying to make a db connection using postgresql and python2.7
> when i type import postgresql and enter I get the following error:
>
> Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
>
> >>> import postgresql
>
> Traceback (most recent call last):
>   File "<pyshell#0>", line 1, in <module>
>     import postgresql
> ImportError: No module named postgresql
>
> Help me plz

You probably want to import psycopg2. I don't believe that there is a
module called postgresql. Or if there is, it will not be usable with
Django. If you are using Django, you should set the DATABASES in
settings.py correctly, and just use "from django.db import connection"
and use that. See the documentation for how to do this.

I understand you are a beginner and things can seem cryptic at first.
However, I must encourage you to solve these issues on your own as
much as possible. For example the first problem you had was pretty
well pointed out to you by Python's exception message:
TypeError at /time/plus/1
'tuple' object is not callable
Exception location: mysite\urls.py in <module>, line 6

- Anssi

kalyani ram

unread,
Feb 20, 2012, 4:39:35 AM2/20/12
to Django users
Thank you. :) i ll do my best and sort this issue out :)
Reply all
Reply to author
Forward
0 new messages