0

我正在写一个小应用程序。我希望使用我找到的视图装饰器。这是片段,这是描述其用法的帖子。我有一个 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!")

谢谢!

4

1 回答 1

1

装饰器接受一个参数,所以你必须调用装饰器,即使你不提供参数realm并让它默认为“”。

@logged_in_or_basicauth()
def test_handler(request):
    return HttpResponse("Test!")
于 2013-07-23T21:13:15.800 回答