【问题标题】:TypeError: int() argument must be a string, a bytes-like object or a number errorTypeError: int() 参数必须是字符串、类似字节的对象或数字错误
【发布时间】:2017-07-02 08:15:43
【问题描述】:

我想将数据保存在我的数据库中,但我的代码中有 TypeError。 实际错误是:

TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是 'QueryDict'

所以请帮我解决这个问题。 以下是我在 Views.py 上的代码:

    card = Cards_detail(request.POST)
    card.user_id = int((request.user.id))
    card.card_no = int(request.POST['card-number'])
    card.expiray_month = request.POST['expiry-month']
    card.expiray_year = request.POST['expiry-year']
    card.card_type = 'Visa'  #temp value change this value as per api
    #card.bank_id = 1         # temp value change this value as per api
    card.save()    # Saving Card Details

【问题讨论】:

  • 实际错误是:-TypeError: int() 参数必须是字符串、字节类对象或数字,而不是'QueryDict'
  • 请尝试打印 request.POST['card-number'].

标签: python django django-views


【解决方案1】:

问题出在这一行:

 card = Cards_detail(request.POST) 

在使用模型之前,您不能将模型 Cards_detailrequest.POST 一起使用。 您需要使用 objects.create

if request.POST: 
    card_no = int(request.POST.get('card-number')) 
    expiray_month = request.POST['expiry-month'] 
    expiray_year = request.POST['expiry-year'] 
    card_type = 'Visa'

    card = Cards_deatil.objects.create(user = request.user,card_no = card_no,expiray_month = expiray_month,expiray_year = expiray_year,card_type = 'Visa') 
    card.save()

    return HttpResponse('Done')

【讨论】:

  • 我已经这样做了,但它不起作用仍然给我同样的错误。我很困惑 user_id 或 card_no 应该在哪里出错??
  • @AmazeLife 向我们展示了整个追溯。请打印问题中错误的总回溯。这样我才能更好地帮助你。
  • @AmazeLife 请向我展示整个视图,而不仅仅是这 5 行。请将您的总代码粘贴到问题中。
  • 文件 "D:\Python\Cashwalapp\cashwalappsecondary\Cwallet\views.py",第 54 行,在 addmoney card.save() # 保存卡详细信息
  • 文件 "D:\Python\Cashwalapp\ENV\lib\site-packages\django\db\models\fields_init_.py",第 946 行,在 get_prep_value 返回整数(值)
猜你喜欢
  • 2021-08-20
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 2018-03-17
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多