Django:基数为10的int()的文字无效:'数字'

时间:2017-08-04 08:40:24

标签: python django sqlite

这是我的代码:

** urls.py: **

from django.conf.urls import url, include
from base_app import views
from django.contrib.auth import views as auth_views

app_name = 'base_app'

urlpatterns = [
   url(r'^forms/$', views.form, name='form'),
   url(r'^tables/$', views.table, name='table'),
   url(r'^register/', views.register, name='register'),
   url(r'^login/$', views.user_login, name='login'),
   url(r'^logout/$', views.user_logout, name='logout'),# line added
]

** views.py: **

def form(request):
    registered = False
    if request.method == 'POST':
        user_form = FormName(data=request.POST)
        if user_form.is_valid():
            user = user_form.save()
            user.save()
            registered = True
        else:
            print(user_form.errors)
    else:
        user_form = FormName()
        return render(request, 'base_app/form-samples.html', {'user_form':user_form, 'registered':registered})

    return render(request, 'base_app/form-samples.html')

** models.py: **

class InfoForm(models.Model):

    username = models.CharField(max_length=256, default='username')
    email = models.EmailField(max_length=256, default='email')
    number = models.PositiveIntegerField(max_length=256, null=True)

    def __str__(self):
        return self.user.username

** forms.py: **

class FormName(forms.ModelForm):

    class Meta:
        model = InfoForm
        fields = ('username', 'email', 'number')

之后我跑,python manage.py migrate

我收到此消息 - > ValueError: invalid literal for int() with base 10:'号码'`

问题出在哪里?

1 个答案:

答案 0 :(得分:1)

PositiveIntegerField不支持max_length选项。它的范围是0到2147483647.

from django.core.validators import MaxValueValidator
...
number = models.PositiveIntegerField(validators=[MaxValueValidator(256)])