简体   繁体   English

django-login-required 中间件给出类型错误

[英]django-login-required middleware giving type error

In my django project I am trying to use the django-login-required-middleware .在我的 django 项目中,我正在尝试使用django-login-required-middleware Upon running the server, I get the following error message:运行服务器时,我收到以下错误消息:

TypeError at /

'bool' object is not callable
Traceback (most recent call last):
  File "/home/krishnan/anaconda3/envs/django_env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/krishnan/anaconda3/envs/django_env/lib/python3.8/site-packages/login_required/middleware.py", line 26, in __call__
    is_authenticated = request.user.is_authenticated()

Exception Type: TypeError at /
Exception Value: 'bool' object is not callable

When I go to line 26 of the file site_packages/login_required/middleware.py , I see the following code:当我 go 到文件site_packages/login_required/middleware.py的第 26 行时,我看到以下代码:

# Django v2 now has request.user.is_authenticated as a boolean instead
#       of a function that returns a boolean
if django.VERSION[0] == 2:
    is_authenticated = request.user.is_authenticated
else:
    is_authenticated = request.user.is_authenticated()

I am using Django 3.1.6.我正在使用 Django 3.1.6。 I installed the middleware on 04 June 2021 with pip as instructed in the pypi page, and I presume it is the latest version(the middleware.py file does not show version info).我在 2021 年 6 月 4 日按照 pypi 页面中的说明使用 pip 安装了中间件,我认为它是最新版本( middleware.py文件不显示版本信息)。 As per the pypi page the middleware version is 0.6.1 and supports Django 3.0 also.根据 pypi 页面,中间件版本为 0.6.1,并且还支持 Django 3.0。 But from the above code snippet, it would seem that the middleware code checks only for Django version 2 to use the is_authenticated property instead of the is_authenticated() function, and could be the reason for the error.但是从上面的代码片段看来,中间件代码似乎只检查 Django 版本 2 以使用is_authenticated属性而不是is_authenticated() function,这可能是错误的原因。 Am I correct?我对么? Or could the error be caused by some other reason?或者错误可能是由其他原因引起的?

Update the middleware or fix if condition yourself as Django 3 routes you to else which is `is_authenticated()' like 1.11 which fails更新中间件或修复如果将自己设置为 Django 3 将您路由到其他类似 1.11 的 `is_authenticated()' 失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM