0

我想编写一个测试用例,用于将发布数据发送到登录页面。这没用。我在这里发布我的代码,希望你能帮助我。谢谢。

 def setUp(self):
    """set up"""
    un = 'abc@gmail.com'
    pw = '123'
    self.user = User.objects.create_user(un, un)
    self.user.is_staff = True
    self.user.is_superuser = True
    self.user.firstname = "John"
    self.user.lastname = "Smith"
    self.user.password = '123'
    self.user.save()
    print '*** password: ', self.user.password


def testPost(self):
    """test POST requests"""
    post_data = {
        'email': 'abc@gmail.com',
        'password': '123',
    }         

    response = self.client.post(reverse('myapp_home', post_data))        
    print response.status_code

错误输出如下。

 ERROR: testPost (submngr.tests.model_tests.model_tests.FormsTestCase)
test POST requests
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/model_tests/model_tests.py", line 117, in testPost
    response = self.client.post('/', post_data)
  File "/usr/local/lib/python2.7/dist-packages/django/test/client.py", line 449, in post
    response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
  File "/usr/local/lib/python2.7/dist-packages/django/test/client.py", line 262, in post
    return self.request(**r)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "views.py", line 84, in homepage
    print results[0].check_password(form.cleaned_data['password'])
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py", line 304, in check_password
    return check_password(raw_password, self.password, setter)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/hashers.py", line 42, in check_password
    hasher = get_hasher(algorithm)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/hashers.py", line 115, in get_hasher
    "setting?" % algorithm)
ValueError: Unknown password hashing algorithm '123'. Did you specify it in the PASSWORD_HASHERS setting?
4

1 回答 1

1

您已将用户密码直接存储为纯字符串self.user.password = 123,但 django 使用散列算法存储用户密码,这就是您收到错误的原因。您可以使用用户方法设置用户密码,该set_password方法将在保存之前应用哈希算法:

user.set_password('123')
user.save()
于 2013-02-01T17:22:51.060 回答