TypeError:'str'对象不可调用(Django / Python)

时间:2012-05-30 02:29:17

标签: python django fullcalendar

我正在尝试为我的应用程序构建一个REST类型的JSON API,当我测试它时,当我点击我的页面的URL时,我一直收到一个神秘的错误。

URL配置:

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

views.py:

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

@json_view
def calendar_resource(request, id, month, year):
    if id != request.user.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

我正在将QuerySet结果转换为列表,因此我可以对它进行更多操作(即插入不会在查询中返回的记录),然后再将列表作为JSON传递给fullCalendar进行处理。

ETA:这是导致我使用此实现的original question

追溯:

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

Django Version: 1.3.1
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.humanize',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'lessons',
 'registration']
Installed Middleware:
('django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" 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

3 个答案:

答案 0 :(得分:4)

在您的URLConf中,它应该是'views.calender_resource',而不仅仅是'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'),
)

其中APP_NAME是此视图所属应用的名称。


reference

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

您可以使用url()函数而不是元组作为patterns()的参数。如果要指定不带可选额外参数字典的名称,这很方便。例如:

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

此函数有五个参数,其中大多数是可选的:

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

答案 1 :(得分:1)

你能提供完整的追溯吗?

到目前为止的问题:

  1. 您不能直接在模型实例列表上json.dumps。如果您想要这样的转储,请查看django.core.serializers

    from django.core.serializers.json import Serializer
    Serializer().serialize(Lesson.objects.filter(...))
    
  2. 'application/json'是json的标准mime-type而不是'text/json'

答案 2 :(得分:0)

检查field lookup syntax。你需要将kwargs传递给filter()。这意味着使用__lt类型语法而不是标准的python比较运算符。

l = Lesson.objects.filter(student__teacher=request.user).filter(startDate__lte= thisMonth).filter(endDate__gte=thisMonthStart)