return int(value)ValueError:int()的无效字面值为10:'' [英] return int(value) ValueError: invalid literal for int() with base 10: ''

查看:127
本文介绍了return int(value)ValueError:int()的无效字面值为10:''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


python manage.py migrate


上面提到的错误即将到来。我不知道我在哪里错了。
我的populate_rango.py文件是

  import os 
os.environ.setdefault('DJANGO_SETTINGS_MODULE' 'tango_with_django_project.settings')


import django
django.setup()

from rango.models import类别,页


def populate():
python_cat = add_cat('Python')

add_page(cat = python_cat,
title =官方Python教程
url =http://docs.python.org/2/tutorial/)

add_page(cat = python_cat,
title =如何看待电脑科学家,
url =http://www.greenteapress.com/thinkpython/)

add_page(cat = python_cat,
title =在十分钟内学习Python
url =http://www.korokithakis.net/tutorials/python/)

django_cat = add_cat(Django)

add_page (cat = django_cat,
title =官方Django教程,
url =https://docs.djangoproject.com/en/1.5/intro/tutorial01/)

add_page(cat = django_cat,
title =Django Rocks,
url =http://www.djangorocks.com/)

add_page(cat = django_cat ,
title =如何用Django探戈,
url =http://www.tangowithdjango.com/)
frame_cat = add_cat(Other Frameworks)

add_page(cat = frame_cat,
title =Bottle,
url =http://bottlepy.org/docs/dev/)

add_page(cat = frame_cat,
title =Flask,
url =http://flask.pocoo.org)

#打印我们添加的内容用户。
在Category.objects.all()中的c:
在Page.objects.filter(category = c)中:
打印 - {0} - {1}格式(str(c),str(p))

def add_page(cat,title,url,views):
p = Page.objects.get_or_create(category = cat,title = title) [0]
p.url = url
p.views = views
p.save()
return p

def add_cat(name,views ,喜欢):
c = Category.objects.get_or_create(name = name)[0]
c.views = views
c.likes = likes
c.save()
return c

#在这里开始执行!
如果__name__ =='__main__':
打印启动Rango人口脚本...
填充()

错误是

 应用rango.0002_auto_20151102_1020 ...追溯(最近的调用last):
文件manage.py,第10行,在< module>
execute_from_command_line(sys.argv)
文件/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py,第351行,在execute_from_command_line
utility.execute()
文件/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py,第343行,执行
self.fetch_command (子命令).run_from_argv(self.argv)
文件/usr/local/lib/python2.7/dist-packages/django/core/management/base.py,第394行,在run_from_argv
self.execute(* args,** cmd_options)
文件/usr/local/lib/python2.7/dist-packages/django/core/management/base.py,第445行,执行
output = self.handle(* args,** options)
文件/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py,行222,在句柄
executor.migrate(targets,plan,fake = fake,fake_initial = fake_initial)
文件/usr/local/lib/python2.7/dist-packages/django/db/迁移/ executor.py,第110行,迁移
self.apply_migration(states [migration ],migration,fake = fake,fake_initial = fake_initial)
文件/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py,第148行在apply_migration
state = migration.apply(state,schema_editor)
文件/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py,第115行,适用
operation.database_forwards(self.app_label,schema_editor,old_state,project_state)
文件/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py ,line 62,in database_forwards
field,
File/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py,第179行,在add_field
self._remake_table(model,create_fields = [field])
文件/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py ,第77行,_remake_table
self.effective_default(field)
文件/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py ,line 211,in effective_default
default = felel d.get_db_prep_save(default,self.connection)
文件/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py,第710行,在get_db_prep_save
prepared = False)
文件/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py,第702行,get_db_prep_value
value = self.get_prep_value(value)
文件/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py,第1868行,在get_prep_value
return int(value)
ValueError:int()的无效字面值为10:''

0002_auto_20151102_1020文件是

 从__future__导入unicode_literals 

从django.db导入迁移,模型


class Migration(migrations.Migration):

dependencies = [
('rango','0001_initial'),
]

operations = [
migrations.AddField(
model_name ='category',
name = 'like',
field = models.IntegerField(default = 1),
preserve_default = False,
),
migrations.AddField(
model_name ='category' ,
name ='views',
field = models.IntegerField(default =''),
preserve_default = False,
),
]

models.py是

 从django.db导入模型

#在这里创建您的模型。
类别(models.Model):
name = models.CharField(max_length = 128,unique = True)
views = models.IntegerField()
likes = models.IntegerField ()
def __unicode __(self):#For Python 2,在Python 3上使用__str__
return self.name

class Page(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length = 128)
url = models.URLField()
views = models.IntegerField()

def __unicode __(self):#For Python 2,在Python 3上使用__str__
return self.title

请帮助我。谢谢你花时间...

解决方案

你有

  migrations.AddField(
model_name ='category',
name ='views',
field = models。 IntegerField(default =''),#< ----问题行
preserve_default = False,
),

IntegerField 需要整数,而不是字符串;)



类型模型,您需要

  views = models.IntegerField(default = 0)

然后再次运行命令:

  makemigrations 
migrate


I am new to djnago.When I am about to run the command

python manage.py migrate

above mentioned error is coming.I don't know where I am wrong. My populate_rango.py file is

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE',   'tango_with_django_project.settings')


import django
django.setup()

from rango.models import Category, Page


def populate():
   python_cat = add_cat('Python')

   add_page(cat=python_cat,
     title="Official Python Tutorial",
     url="http://docs.python.org/2/tutorial/")

   add_page(cat=python_cat,
     title="How to Think like a Computer Scientist",
     url="http://www.greenteapress.com/thinkpython/")

   add_page(cat=python_cat,
     title="Learn Python in 10 Minutes",
     url="http://www.korokithakis.net/tutorials/python/")

   django_cat = add_cat("Django")

   add_page(cat=django_cat,
     title="Official Django Tutorial",
     url="https://docs.djangoproject.com/en/1.5/intro/tutorial01/")

   add_page(cat=django_cat,
     title="Django Rocks",
     url="http://www.djangorocks.com/")

   add_page(cat=django_cat,
     title="How to Tango with Django",
     url="http://www.tangowithdjango.com/")
  frame_cat = add_cat("Other Frameworks")

   add_page(cat=frame_cat,
     title="Bottle",
     url="http://bottlepy.org/docs/dev/")

   add_page(cat=frame_cat,
     title="Flask",
     url="http://flask.pocoo.org")

# Print out what we have added to the user.
   for c in Category.objects.all():
      for p in Page.objects.filter(category=c):
         print "- {0} - {1}".format(str(c), str(p))

def add_page(cat, title, url, views):
  p = Page.objects.get_or_create(category=cat, title=title)[0]
  p.url=url
  p.views=views
  p.save()
  return p

def add_cat(name,views,likes):
  c = Category.objects.get_or_create(name=name)[0]
  c.views=views
  c.likes=likes
  c.save()
  return c

# Start execution here!
if __name__ == '__main__':
  print "Starting Rango population script..."
  populate()

the error is

 Applying rango.0002_auto_20151102_1020...Traceback (most recent call last):
 File "manage.py", line 10, in <module>
 execute_from_command_line(sys.argv)
 File "/usr/local/lib/python2.7/dist-packages/django/core/management  /__init__.py", line 351, in execute_from_command_line
utility.execute()
 File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 343, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table
self.effective_default(field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 211, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 702, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1868, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: ''

0002_auto_20151102_1020 file is

from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

   dependencies = [
      ('rango', '0001_initial'),
    ]

   operations = [
      migrations.AddField(
          model_name='category',
          name='likes',
          field=models.IntegerField(default=1),
          preserve_default=False,
       ),
      migrations.AddField(
          model_name='category',
          name='views',
          field=models.IntegerField(default=''),
          preserve_default=False,
      ),
]

models.py is

 from django.db import models

 # Create your models here.
 class Category(models.Model):
    name = models.CharField(max_length=128, unique=True)
    views=models.IntegerField()
    likes=models.IntegerField()
    def __unicode__(self):  #For Python 2, use __str__ on Python 3
        return self.name

 class Page(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=128)
    url = models.URLField()
    views = models.IntegerField()

    def __unicode__(self):      #For Python 2, use __str__ on Python 3
        return self.title

Please help me.Thanks for spending you time...

解决方案

you have

migrations.AddField(
      model_name='category',
      name='views',
      field=models.IntegerField(default=''), # <---- problem line
      preserve_default=False,
),

IntegerField needs integer, not strings ;)

in your Category model, you need

views = models.IntegerField(default=0)

and then run the commands again:

makemigrations
migrate

这篇关于return int(value)ValueError:int()的无效字面值为10:''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆