python - 异常: invalid literal for int() with base 10: ''

标签 python django

我正在使用模型来保存用户信息,例如个人资料图片和个人简介等,但是当我注册时会发生此错误。 这是代码

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete='CASCADE')
    description = models.CharField(max_length = 500, default='', blank = True)
    city = models.CharField(max_length = 100, default='', blank = True)
    website = models.URLField(default='', blank = True)
    phone = models.IntegerField(default='', blank = True)
    image = models.FileField()

@receiver(post_save, sender = User)
def create_profile(sender, instance, created, **kwargs):
        if created:
            UserProfile.objects.create(user=instance)
        instance.userprofile.save()

最佳答案

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

表示不能做Type Conversion'' .

我想phone = models.IntegerField(default='', blank = True)这是问题所在。

因为你没有给 phone 赋值,所以它使用默认值 '' ,不能通过 int() 进行转换在Python中。

关于python - 异常: invalid literal for int() with base 10: '' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50834317/

相关文章:

python - 导入错误 : libOpenGL. so.0:无法打开共享对象文件:没有这样的文件或目录

Python 线程子类 : __init__ not called

python - 无法访问在 docker 上构建的容器中运行的 Web 应用程序

python - PyAudio 和分贝变化和平移

javascript - 在 Javascript 中使用 Mako 模板

Python (Windows Server) Virtualenv 无法识别 Django 已安装

python - Django 从列表中自动填充表单

django - Django : search_device_list() missing 1 required positional argument: 'id'

django - GAE 和 django : static files

python - Django Rest Framework 中的空路由