- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在学习 Flask Mega-Tutorial,我遇到了这段代码:
class User(db.Model):
id = db.Column(db.Integer, primary_key = True)
nickname = db.Column(db.String(64), unique = True)
email = db.Column(db.String(120), unique = True)
role = db.Column(db.SmallInteger, default = ROLE_USER)
posts = db.relationship('Post', backref = 'author', lazy = 'dynamic')
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return unicode(self.id)
def __repr__(self):
return '<User %r>' % (self.nickname)
is_authenticated、is_active 和 is_anonymous 对我来说似乎很奇怪 - 他们什么时候会返回除预定义值之外的任何内容?
有人可以向我解释为什么 Flask-Login 让我使用这些看似无用的方法吗?
最佳答案
首先,is_anonymous()
和is_authenticated()
互为逆。如果需要,您可以将一个定义为另一个的否定。
您可以使用这两种方法来确定用户是否已登录。
当没有人登录时,Flask-Login 的 current_user
被设置为一个 AnonymousUser
对象。此对象以 False
响应 is_authenticated()
和 is_active()
并以 响应
。is_anonymous()
是的
is_active()
方法还有另一个重要的用途。不要像我在教程中建议的那样总是返回 True
,您可以让它为被禁止或停用的用户返回 False
,并且不允许这些用户登录。
关于python - Flask-Login 中使用的 "is_authenticated"方法有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19532372/
我正在使用 django-email-as-username使用电子邮件而不是用户名进行身份验证的应用程序,它的作用就像一个魅力。 现在,在 查看 我想打电话 if request.user.is_a
我正在我的 views.py 中检查用户是否经过身份验证。然而,Django 不断抛出异常 WSGIRequest 对象没有属性“is_authenticated”。我阅读了 stackoverflo
我正在使用 Flask-Login,我想通过调用 current_user.is_authenticated() 检查用户是否已登录。 我的 Flask 应用程序被组织成蓝图,其中包含一个主要的 __
在我的模板中,我有以下内容: {% if user.is_authenticated %} My Account
我正在尝试编写一个用于在 Django 中注销用户的测试。这是代码:urls.py from django.conf.urls import url from django.contrib impor
我的大部分 View 都需要登录。有没有办法指定所有 View 或 View 子集在继续之前必须让用户 is_authenticated() = True。如果没有,用户被重定向到登录页面? 最佳答案
我正在使用一个测试单元来验证 Flask-Login 的登录。这样做的目的是验证凭据不正确的用户的行为: def test_login_user(self): '''test
我正在查看 this website但似乎无法弄清楚如何做到这一点,因为它不起作用。我需要检查当前站点用户是否已登录(已通过身份验证),并且正在尝试: request.user.is_authenti
我刚刚安装了 django all-auth 模块,我的问题是用户 session 始终为 false。 user.is_authenticated 总是给我 false。我注册并使用我的凭据登录,然
我正在努力解决 is_authenticated返回 True当我没有登录时: u = request.user if u.is_authenticated: raise Exception(
这个问题已经有答案了: Flask-Login raises TypeError: 'bool' object is not callable when trying to override is_a
在我的模板 login.html 中,我有: {% if form.errors %} {% if user.is_authenticated %} Your account does
使用 GAE/Django-nonrel 创建简单的应用程序(我认为问题不是特定于 GAE 或 nonrel 分支,最像 PEBKAC 作为 python/django noob,并且会在基本的 dj
在我的 base.html 中,user.is_authenticated 总是返回 false... 我希望基本模板在“登录和注销”之间切换 帮助将不胜感激 基础.html {%
我正在尝试将模板标签/包含标签合并到网站的侧边栏中。当我输入时,页面的主要部分会正确更新: {% if user.is_authenticated %} Hello {{ user.userna
我只是想运行一个简单的 {% if user.is_authenticated %} 。但它总是返回 False。 这是我所有的文件。 views.py from django.shortcuts i
谁能告诉我为什么在下面的代码中我被重定向到 yahoo.com 而不是 google.com? 网址 urlpatterns = patterns('', (r'^$', initialRequest
我正在学习 Flask Mega-Tutorial,我遇到了这段代码: class User(db.Model): id = db.Column(db.Integer, primary_key
我没有看到使用 @login_required 装饰器和 is_authenticated() 之间的明显区别:不知何故,我认为它们执行类似的检查(虽然不完全)。 假设我的 views.py 中有一个
我有一个奇怪的问题,在我的设置文件中,所有需要为模板中的 user.is_authenticated 使用启用的内容都已启用。 我有以下模板标签代码: from django import templ
我是一名优秀的程序员,十分优秀!