【问题标题】:'int' object has no attribute 'encode' api json data django'int' 对象没有属性 'encode' api json data django
【发布时间】:2017-04-10 10:53:47
【问题描述】:

我通过 json 中的 post 请求发送数据,但在视图中没有获取密码

JsonFormet

{"email": "xyz@gmail.com", "password":123456 }

我收到这样的电子邮件 ID

email = request.data['email']
password = request.data['password']

但没有得到密码

Error

'int' object has no attribute 'encode'

事件我尝试编码但仍然出现错误

password = request.data['password'].encode('utf-8')

【问题讨论】:

    标签: json django-rest-framework


    【解决方案1】:

    这是因为您在 JSON 中传递了一个整数,而您稍后没有将其转换为字符串,而 Django 需要一个字符串作为密码。

    所以请确保将密码转换为字符串:

    password = str(request.data['password'])
    

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 2015-12-30
      • 2013-08-05
      • 2018-10-16
      • 2018-06-02
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多