django value eRror无效的int()base 10:''如何解决无类型错误

发布于 2025-02-06 03:47:46 字数 783 浏览 1 评论 0原文

以django表单添加值时,从old_car字段中获取 valueerror无效的int()to in int()base 10:''

new_car和old_car的值是字符串,需要转换为Integer,以进一步条件,我知道如果我删除int将解决错误:)。

我如何解决该

django forms.py

car = cleaned_data.get("user", ['AUTO'])
if car[0] == 'AUTO':
    records = []
    for i in range(count):
        new_car = int(self.data.get(f'cardefination_set-{i}-new_car', []))
        old_car =int(self.data.get(f'cardefination_set-{i}-old_car', []))
        records.append((new_car, old_car))
    records = sorted(records, key=lambda record: record[1])

Trackback

https://stackoverflow.com/posts/72548081/edit#

in Django forms when i am appending the values , from old_car field getting ValueError invalid literal for int() with base 10: ''

the values of new_car and old_car are string and need to convert to integer for further conditions, i know if i remove int will solve the error :).

how i can solve that

django forms.py

car = cleaned_data.get("user", ['AUTO'])
if car[0] == 'AUTO':
    records = []
    for i in range(count):
        new_car = int(self.data.get(f'cardefination_set-{i}-new_car', []))
        old_car =int(self.data.get(f'cardefination_set-{i}-old_car', []))
        records.append((new_car, old_car))
    records = sorted(records, key=lambda record: record[1])

traceback

https://stackoverflow.com/posts/72548081/edit#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和我恋爱吧 2025-02-13 03:47:46

在不了解您的代码的情况下,我将解决与这些行相关的几个问题:

#...
new_car = int(self.data.get(f'cardefination_set-{i}-new_car', []))
old_car =int(self.data.get(f'cardefination_set-{i}-old_car', []))
#...

这两行中的问题都是相同的。您是casting(显式类型转换)使用int使用int(value)。这意味着该值必须“转换”到int。在您的情况下,您将从self.data中获得一个值,我将假设它是字典。如果无法将获得的值转换为int,您将获得该错误。

此外,我看到您在dict.get(键,值)函数上使用默认值参数。

获取(键[,默认])

如果键在字典中,则返回键的值,否则默认值。如果未给出默认值,则默认为none,因此此方法永远不会提出键盘。

您提供的默认值是[]list ie int(int([])将投掷:

typeError:int()参数必须是字符串,类似字节的对象或真实数字,而不是“列表”

该错误不是您提到的错误,而只是想指出这一点。

我认为您的问题是您从字典中获得的价值是一个空字符串

因此,您的错误:

base 10的int()int()的value eRror无效文字:''

确保您从dict获得的东西可以转换为int

Without knowing more about your code I will address a couple of issues related with these lines:

#...
new_car = int(self.data.get(f'cardefination_set-{i}-new_car', []))
old_car =int(self.data.get(f'cardefination_set-{i}-old_car', []))
#...

The issue is the same in both lines. You are casting (explicit type conversion) a value to int using int(value). This means that the value must be "convertible" to int. In your case you are getting a value from self.data, that I am going to assume it is a dictionary. If the obtained value can't be converted to int you will get that error.

Moreover, I see you are using the default value parameter on the dict.get(key, value) function.

get(key[, default])

Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

The default value you are providing is [] which is a list, i.e. int([]) will throw:

TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'

That error is not the error you are mentioning, but just wanted to point that out.

I think your issue is that the value you are getting from the dictionary is an empty string.

Hence your error:

ValueError invalid literal for int() with base 10: ''

Make sure what you are getting from the dict can be converted to int

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文