1

我使用 Django 自定义用户模型。在管理员中一切正常 - 登录、注销等。但是当我尝试从我自己的 view.py 调用登录时:

def my_login(request, *args, **kwargs):
    response = login(request, *args, **kwargs)
    return response

我收到以下错误:

AssertionError at /cust/login/
sensitive_post_parameters didn't receive an HttpRequest. If you are decorating a classmethod, be sure to use @method_decorator.

Request Method: POST
Request URL: http://localhost:8000/cust/login/
Django Version: 1.6.1
Python Version: 2.7.3

Traceback:
File "virtual_env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "virtual_env/local/lib/python2.7/site-packages/django/views/generic/base.py" in view 69. return self.dispatch(request, *args, **kwargs)
File "virtual_env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view 57.    return view_func(*args, **kwargs)
File "virtual_env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch 399.response = self.handle_exception(exc)
File "virtual_env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch 396.response = handler(request, *args, **kwargs)
File "virtual_env/local/lib/python2.7/site-packages/rest_framework/decorators.py" in handler 49.return func(*args, **kwargs)
File "virtual_env/myproject/myidentity/views.py" in my_login 13.response = login(request, *args, **kwargs)
File "virtual_env/local/lib/python2.7/site-packages/django/views/decorators/debug.py"in sensitive_post_parameters_wrapper 68."sensitive_post_parameters didn't receive an HttpRequest. If you "

Exception Type: AssertionError at /cust/login/
Exception Value: sensitive_post_parameters didn't receive an HttpRequest. If you are decorating a classmethod, be sure to use @method_decorator.
Request information:
GET: No GET data

没有找到关于这个问题的太多参考,除了这个: Django AssertionError "sensitive_post_parameters didn't receive an HttpRequest" on add users in admin

谢谢

4

1 回答 1

0

解决方法。我不是 django 专家,很可能这不是最健康的解决方案,但是深入研究登录过程的过程中,我在 Django 站点包中遇到了这个断言命令,它阻止了它下面的代码看起来很合理,在评论断言之后,它工作得很好。

virtual_env > lib > python2.7 > site-packages > django > views > decorators > debug.py

def decorator(view):
        @functools.wraps(view)
        def sensitive_post_parameters_wrapper(request, *args, **kwargs):
            '''
            assert isinstance(request, HttpRequest), (
              "sensitive_post_parameters didn't receive an HttpRequest. If you "
              "are decorating a classmethod, be sure to use @method_decorator."
            )
            '''
            if parameters:
                request.sensitive_post_parameters = parameters
            else:
                request.sensitive_post_parameters = '__ALL__'
            return view(request, *args, **kwargs)
        return sensitive_post_parameters_wrapper
    return decorator
于 2014-03-07T16:19:23.640 回答