我设置 if 语句来查看当前用户是否设置了密码。由于某种原因,它只是行不通。我试过:
{% if not user.password %}
{% if user.password == None %}
{% if user.password is None %}
我有 2 个用户帐户(打开不同的浏览器),一个有密码,另一个没有。当我使用上面的语句时,我在两个浏览器中得到了相同的显示。我做错了什么?
最佳答案
>>> a = User.objects.create_user('user1', 'user1@example.com')
>>> b = User.objects.create_user('user2', 'user2@example.com', password='secret')
>>> a.has_usable_password()
False
>>> b.has_usable_password()
True
更新:
根据文档,has_usable_password
的行为发生了变化。
Changed in Django 2.1:
In older versions, this also returns False if the password is None or an empty string, or if the password uses a hasher that’s not in the PASSWORD_HASHERS setting. That behavior is considered a bug as it prevents users with such passwords from requesting a password reset.
关于python - 检查 Django 用户是否设置了密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17250226/