django - 为什么'bool'对象不可调用
问题描述
这是我在 django 中的中间件文件
import re
from django.conf import settings
from django.shortcuts import redirect
EXEMPT_URL = [re.compile(settings.LOGIN_URL.lstrip('/'))]
if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
EXEMPT_URL += [re.compile(url)for url in settings.LOGIN_EXEMPT_URLS]
class LoginRequiredMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self,request):
response = self.get_response(request)
return response
def process_view(self,request, view_func, view_args, view_kwargs):
assert hasattr(request,'user')
path = request.path_info
print(path)
if not request.user.is_authenticated():
if not any (url.match(path) for url in EXEMPT_URL):
return redirect(settings.LOGIN_URL)
url_is_exempt = any (url.match(path) for url in EXEMPT_URL)
if request.user.is_authenticated() and url_is_exempt:
return redirect(settings.LOGIN_REDIRECT_URL)
elif request.user.is_authenticated() or url_is_exempt:
return None
else:
return redirect(settings.LOGIN_URL)
这是我的错误:如果不是 request.user.is_authenticated(): TypeError: 'bool' object is not callable 请有人帮我
解决方案
只是改变
if not request.user.is_authenticated():
至
if not request.user.is_authenticated:
正如上面评论中提到的,is_authenticated
是一个属性,而不是一个函数。所以你不能调用它。
推荐阅读
- javascript - 没有'type'参数,jquery POST回调不起作用
- typescript - 基于布尔参数的不同返回类型
- c# - 带有 Pomelo MySQL 提供程序的 EF Core 3 不支持视图
- c++ - 用 OpenGL 渲染大量文本的最佳方法是什么
- python - 什么是正则表达式?
- javascript - 有什么方法可以在 Angular 的 ng5-slider 中实现 mouseleave()
- laravel - Laravel 迁移向表中添加字段,但 Laravel 添加了双引号
- r - 绘制 X 轴间隔
- scikit-learn - 如何将标记化中的多词名称保持在一起?
- file - 如何使用 Ansible 在 windows 中查找文件的内容