2013-10-26 54 views
0

林與SQL腳本插入到該數據:無效字面INT()基數爲10: '微' 在Django的網頁

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)的models.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' 

我怎樣才能解決這個問題?

+1

什麼是完整的回溯(查看你的日誌)。 –

+0

完整的追溯,請 – lalo

+0

這裏是什麼'ids'?在行'acc_types_lst = AccountType.objects.filter(pk__in = set(ids))'中。 –

回答

2

似乎有在你的代碼一個錯字,改變

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)) 

因此錯誤。

相關問題