python - 使用帐户身份验证来验证是否已登录

标签 python django authentication view

我有一个小观点:

def AccountHome(request):

    return render(request, 'myapp/account/accounthome.html', {
    })

在之前的 View 中,我使用过:

if user is not None and user.is_active

在使用 native 表单类时检查用户是否已经通过身份验证,例如:AuthenticationForm,例如在登录用户时。

但是在这个 View 上我没有使用它,有没有办法在不再次使用这个 AuthenticationForm的情况下验证用户是否登录?此 View 的目的是在登录时显示主屏幕,因此再次扩展该AuthenticationForm`类似乎不直观。

有什么帮助或想法吗?

谢谢

最佳答案

使用is_authenticated()方法

像这样:if request.user.is_authenticated():

您可以在此处找到引用:https://docs.djangoproject.com/en/dev/ref/contrib/auth/#methods

关于python - 使用帐户身份验证来验证是否已登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19503195/

相关文章:

php - 需要帮助用 cookie 完成登录脚本

asp.net - 使用 URLRouting 从 FormsAuthentication 迁移到 OWIN 后,安全页面上出现 HTTPException - 不重定向到 LoginPath

python - 使用 'exec' 函数在解释器 Shell 中运行脚本

python - 扩展一个以 C 对象作为参数的 Python 函数

python - NumPy 的 : convert 2D array to 3D array

python - 我应该编辑 django 迁移文件来编辑不匹配的依赖项吗

javascript - 序列化 django 模型(用于 js 文件)?

python - ValueError : list. remove(x): x 不在列表中

mysql - Django:你能创建一个与自动生成的直通表的关系吗?

php - md5 和 hash 的问题