Matthew Calabresi Matthew Calabresi - 1 year ago 150
Python Question

TypeError: 'str' object is not callable (Django/Python)

I am attempting to build a REST-type JSON API for my app, and while I'm testing it, I keep getting a cryptic error when I hit the URL of my page.


url(r'^calendar/(?P<id>\d+)/(?P<year>\d+)/(?P<month>\d+)/$', 'calendar_resource'),

def json_view(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return HttpResponse(json.dumps(result), mimetype="text/json")
return wrapper

def calendar_resource(request, id, month, year):
if id !=
return HttpResponseForbidden()
thisMonthEnd = datetime.datetime(year, month, calendar.mdays[month])
thisMonthStart = datetime.datetime(year, month, 1)
l = Lesson.objects.filter(student__teacher = request.user).filter(startDate__lte=thisMonthEnd).filter(endDate__gte=thisMonthSta‌​rt)
lessonList = list(l)
return lessonList

I'm converting the QuerySet result to a list so I can do more operations on it (i.e. insert records that wouldn't be returned in the query) before passing the list back as JSON for processing by fullCalendar.

ETA: This is the original question that led me to use this implementation.


Request Method: GET
Request URL: http://localhost:5678/calendar/1/2012/5/

Django Version: 1.3.1
Python Version: 2.6.5
Installed Applications:
Installed Middleware:

File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)

Exception Type: TypeError at /calendar/1/2012/5/
Exception Value: 'str' object is not callable

Answer Source

In your URLConf, it should be 'views.calender_resource' instead of just 'calender_resource'.


urlpatterns = patterns('',

    url(r'^calendar/(?P<id>\d+)/(?P<year>\d+)/(?P<month>\d+)/$', 'APP_NAME.views.calendar_resource'),


urlpatterns = patterns('APP_NAME.views',

    url(r'^calendar/(?P<id>\d+)/(?P<year>\d+)/(?P<month>\d+)/$', 'calendar_resource'),

where APP_NAME is the name of the app this view belongs to.

For reference:

url(regex, view, kwargs=None, name=None, prefix='')

You can use the url() function, instead of a tuple, as an argument to patterns(). This is convenient if you want to specify a name without the optional extra arguments dictionary. For example:

urlpatterns = patterns('',
    url(r'^index/$', index_view, name="main-view"),

This function takes five arguments, most of which are optional:

url(regex, view, kwargs=None, name=None, prefix='')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download