以 10 为基数的 int() 的无效文字:Django 网页上的“Micro”
Posted
技术标签:
【中文标题】以 10 为基数的 int() 的无效文字:Django 网页上的“Micro”【英文标题】:invalid literal for int() with base 10: 'Micro' at Django web-page 【发布时间】:2013-11-05 18:14:05 【问题描述】:我用 sql-script 插入了这个数据:
INSERT INTO "siteDB_accounttype" VALUES
(1,'Micro'),
(2,'Standart'),
(3,'МТ-ECN');
并在 Django 中使用此代码(使用 Python 2.7): 1)views.py
def registration(request):
reg_form = RegisterForm()
title = 'Регистрация клиента'
return render_to_response('registration.html', locals())
2) forms.py
class RegisterForm(forms.Form):
ids = AccountType.objects.values_list('Name', flat=True)
acc_types_lst = AccountType.objects.filter(pk__in=set(ids))
Name = forms.CharField(max_length=255, required=True, label='Имя')
Surname = forms.CharField(max_length=255, required=True, label='Фамилия')
DateOfBirth = forms.DateField(required=True, label='Дата рождения')
Address = forms.CharField(max_length=255, required=True, label='Адрес',
widget=forms.Textarea(attrs='cols': 55, 'rows': 5))
Postcode = forms.IntegerField(min_value=100000, max_value=999999, required=True, label='Почтовый индекс')
Phone = forms.CharField(max_length=50, required=True, label='Телефон')
PassportID = forms.CharField(max_length=50, required=True, label='Номер паспорта')
AccountType = forms.ChoiceField(choices=[AccType for AccType in acc_types_lst],
required=True, label='Тип счета')
DepositeSize = forms.IntegerField(min_value=500, required=True, label='Начальный вклад')
3) 模型.py
from django.utils.encoding import smart_unicode
class AccountType(models.Model):
ID = models.IntegerField(primary_key=True, blank=False)
Name = models.CharField(max_length=50, unique=True, blank=False)
def __unicode__(self):
return smart_unicode(self.Name)
但请使用我的浏览器:
ValueError at /registration/
invalid literal for int() with base 10: 'Micro'
完整的追溯:
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
92. response = middleware_method(request)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/locale.py" in process_request
21. check_path = self.is_language_prefix_patterns_used()
File "/usr/local/lib/python2.7/dist-packages/django/middleware/locale.py" in is_language_prefix_patterns_used
56. for url_pattern in get_resolver(None).url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
347. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
342. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/home/relrin/code/CourseDB/CourseDB/urls.py" in <module>
2. from siteDB.views import *
File "/home/relrin/code/CourseDB/siteDB/views.py" in <module>
5. from siteDB.forms import *
File "/home/relrin/code/CourseDB/siteDB/forms.py" in <module>
6. class RegisterForm(forms.Form):
File "/home/relrin/code/CourseDB/siteDB/forms.py" in RegisterForm
8. acc_types_lst = AccountType.objects.filter(pk__in=set(ids))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in filter
155. return self.get_query_set().filter(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in filter
655. return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in _filter_or_exclude
673. clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in add_q
1266. can_reuse=used_aliases, force_having=force_having)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in add_filter
1197. connector)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/where.py" in add
71. value = obj.prepare(lookup_type, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/where.py" in prepare
339. return self.field.get_prep_lookup(lookup_type, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_prep_lookup
997. return super(IntegerField, self).get_prep_lookup(lookup_type, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_prep_lookup
324. return [self.get_prep_value(v) for v in value]
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_prep_value
991. return int(value)
Exception Type: ValueError at /
Exception Value: invalid literal for int() with base 10: 'MT-ECN'
我该如何解决这个问题?
【问题讨论】:
什么是完整的回溯(在您的日志中查找)。 请完整回溯 这里的ids
是什么?在acc_types_lst = AccountType.objects.filter(pk__in=set(ids))
行中。
【参考方案1】:
你的代码好像有错别字,改一下
class RegisterForm(forms.Form):
ids = AccountType.objects.values_list('Name', flat=True)
到
class RegisterForm(forms.Form):
ids = AccountType.objects.values_list('id', flat=True)
由于打字错误,您稍后会按Name
s(字符串)而不是ID
s(整数)过滤帐户类型列表,
acc_types_lst = AccountType.objects.filter(pk__in=set(ids))
因此出现错误。
【讨论】:
以上是关于以 10 为基数的 int() 的无效文字:Django 网页上的“Micro”的主要内容,如果未能解决你的问题,请参考以下文章
django ValueError:以 10 为基数的 int() 的无效文字:''
ValueError: int() 以 10 为基数的无效文字:'' 使用 tkinter
Youtube-dl 错误:以 10 为基数的 int() 的无效文字:'7766645.83333'
ValueError: int() 以 10 为基数的无效文字:''但在调试器中它有一个值