Django的authenticate方法为一个已经保存的用户返回'None'

问题描述 投票:0回答:1

Settings.py

AUTHENTICATION_BACKENDS = (
        'django.contrib.auth.backends.ModelBackend',
)

Tests.py,其中包含触发错误的代码

class LoginTest(TestCase):

    def test_can_redirect_to_user_page_if_authentificated(self):
        test_user = User(username='foo', password='bar', first_name='Testy', last_name='McTestingTon', email='[email protected]', is_active=True)
        test_user.save()
        current_user = authenticate(username=test_user.username, password=test_user.password)
        print(current_user)

        response = self.client.post('/login', data = {'username' : current_user.username, 'password' : current.password})
        self.assertEqual(response.url, '/users/Testy')

[current_user始终返回为None

我在SO上尝试了其他文章的许多建议,但对于我已将其is_active明确设置为True且将ModelBackend添加到我的settings.py的用户,authentication仍然返回None。

python django django-1.11
1个回答
1
投票
您应该使用create_user创建用户,直接保存create_user对象不会加密密码,因此永远不会进行身份验证

User

© www.soinside.com 2019 - 2024. All rights reserved.