TypeError:'str'对象不可调用(Django / Python) [英] TypeError: 'str' object is not callable (Django/Python)
问题描述
我试图为我的应用程序构建一个REST类型的JSON API,当我测试它时,当我点击我的页面的URL时,我一直收到一个神秘的错误。
URLconf:
url(r'^ calendar /(?P< (+ P< year> \d +)/(?P< month> \d +)/ $','calendar_resource'),
pre>
views.py:
def json_view(func):
def wrapper(* args,** kwargs):
result = func(* args,** kwargs)
return HttpResponse(json.dumps(result) ,mimetype =text / json)
返回包装
@json_view
def calendar_resource(request,id,month,year):$ b $ if if!!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:这是原始问题,导致我使用这个实现。
回溯:
环境:
请求方法:GET
请求URL:http:// localhost:5678 / calendar / 1/2012/5 /
Django版本:1.3.1
Python版本:2.6.5
已安装应用程序:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.humanize',
'django.contrib.sessions' ,
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'课程',
'注册']
安装中间件:
('django.middleware.csrf.CsrfViewMiddleware ,
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
文件/ usr /本地/ lib / python2.6 / dist-packages / django / core / handlers / base.pyin get_response
111. response = callback(request,* callback_args,** callback_kwargs)
异常类型:TypeError at / calendar / 1/2012/5 /
异常值:'str'对象不可调用
$ b $在你的URLConf中,它应该是
'views.calender_resource'
而不是<$ c $
<$> c $ c> urlpatterns = patterns('',
url(r'^ calendar /(?P< id> \ d +)/(?P< ; year> \d +)/(?P\d +)/ $','APP_NAME.views.calendar_resource'),
)
或者:
urlpatterns = patterns('APP_NAME.views' ,
url(r'^ calendar /(P p \\ \\ \\ \\ d +)/(P P year \\ \\ \\ \\ d / / /((($ $ $ $ $ $ $ $ $ $ $ $ ','calendar_resource'),
)
其中APP_NAME是此应用的名称查看属于。
对于参考:
url(正则表达式,视图,kwargs = None,name = None,prefix ='')
您可以使用url()函数而不是元组作为patterns()的参数。如果你想指定一个没有可选的额外参数字典的名字,这很方便。例如:
urlpatterns = patterns('',
url(r' ^ index = $,index_view,name =main-view),
...
)
这个函数有五个参数,其中大部分是可选的:
url(正则表达式,视图,kwargs = None,name = None,prefix ='')
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.
URLconf:
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=thisMonthStart) 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.
Traceback:
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
解决方案In your URLConf, it should be
'views.calender_resource'
instead of just'calender_resource'
.Essentially,
urlpatterns = patterns('', url(r'^calendar/(?P<id>\d+)/(?P<year>\d+)/(?P<month>\d+)/$', 'APP_NAME.views.calendar_resource'), )
Or:
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='')
这篇关于TypeError:'str'对象不可调用(Django / Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!