【问题标题】:No token is generating, with valid username and password using APIclient()没有生成令牌,使用 APIclient() 使用有效的用户名和密码
【发布时间】:2016-10-10 16:48:40
【问题描述】:

我正在尝试从 rest_framework.authtoken.models 导入令牌生成令牌。视图已成功创建,但我正在使用来自 rest_framework.test 导入 APIClient 的 APIClient() 为 post api 编写测试。 def test_address_api_auth_token(self):

    data = {
        "username": self.user.username,
        "password": self.user.password
    }
    self.client.force_authenticate(user=self.user)
    response = self.client.post('/api-token-auth/', data, format='json')
    self.assertEqual(response.status_code, 200)

虽然用户和密码是由模型创建的。但我仍然收到此错误-> AssertionError: 400 != 200。 错误信息 -> 用户名和密码无效。

【问题讨论】:

    标签: python-2.7 django-rest-framework http-post auth-token apiclient


    【解决方案1】:

    Django 存储加密的密码。因此,通过执行self.user.password,您将发送另一个字符串,并且始终无法通过身份验证。

    【讨论】:

      猜你喜欢
      • 2019-12-25
      • 2015-11-17
      • 2011-12-02
      • 2014-12-24
      • 2019-04-22
      • 1970-01-01
      • 2013-05-17
      • 2017-11-01
      相关资源
      最近更新 更多