我正在尝试为我的应用程序构建一个 REST 类型的 JSON API,并且在我测试它时,当我点击我的页面的 URL 时,我不断收到一个神秘的错误。
网址配置:
url(r'^calendar/(?P<id>\d+)/(?P<year>\d+)/(?P<month>\d+)/$', 'calendar_resource'),
视图.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=thisMonthStart)
lessonList = list(l)
return lessonList
我将 QuerySet 结果转换为列表,以便在将列表作为 JSON 传回以供 fullCalendar 处理之前对其执行更多操作(即插入不会在查询中返回的记录)。
ETA:这是导致我使用此实现的原始问题。
追溯:
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