我正在写一个小应用程序。我希望使用我发现的视图装饰器。 这是the snippet,这是a post describing its usage。 我有一台Apache服务器(2.2)在运行,而Django是最新的。 我已经装饰了一个视图,当试图获取它时,会发生一个AttributeError:
Request Method: GET
Request URL: http://localhost/redb/test/
Django Version: 1.5.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'redb_app')
Installed Middleware:
('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 "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
187. response = middleware_method(request, response)
File "C:\Python27\lib\site-packages\django\middleware\common.py" in process_response
109. if response.status_code == 404:
Exception Type: AttributeError at /redb/test/
Exception Value: 'function' object has no attribute 'status_code'
这就是我使用它的方式:
@logged_in_or_basicauth
def test_handler(request):
return HttpResponse("Test!")
谢谢!
答案 0 :(得分:1)
装饰器接受一个参数,所以你必须调用装饰器,即使你没有提供参数realm
并让它默认为“”。
@logged_in_or_basicauth()
def test_handler(request):
return HttpResponse("Test!")