Designer023 Designer023 - 1 year ago 99
Python Question

Checking if a Django user has a password set

I setup if statement to see if the current user has a password set. For some reason it just won't work. I have tried:

{% if not user.password %}
{% if user.password == None %}
{% if user.password is None %}

I have 2 user accounts (different browsers open), one with a password in one, and one without in the other. When I use the statements above I get the same showing in both browsers. What am I doing wrong?

Answer Source

Use user.has_usable_password

>>> a = User.objects.create_user('user1', '')
>>> b = User.objects.create_user('user2', '', password='secret')
>>> a.has_usable_password()
>>> b.has_usable_password()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download