对于带有基数为10的int(),Django错误文字无效:''

时间:2014-05-07 14:49:24

标签: python django

我制作了一个体育预测应用程序,应该可以让人们跟踪他们的数学预测。

到目前为止HTML有48场比赛,他们必须填写每场比赛的分数,但是当我尝试保存分数时,这个错误不断出现:

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

以下是我的观点:

@login_required(login_url="/")
def inicio(request):
    if request.method == "POST":
        form = Pronosticos(request.POST)
        for i in range(48):
            pronostico, _ = PartidosUsuarios.objects.get_or_create(idUsuario=request.user, idPartido=request.POST.get("idPartido"+str(i), ""), PaisL=request.POST.get("PaisL"+str(i), ""), Local=request.POST.get("Local"+str(i), ""), Visita=request.POST.get("Visita"+str(i), ""), PaisV=request.POST.get("PaisV"+str(i), ""), Capturado="Si")
            if pronostico.Local > pronostico.Visita:
                pronostico.Resultado = "Local"
                pronostico.save()
            elif pronostico.Visita > pronostico.Local:
                pronostico.Resultado = "Visita"
                pronostico.save()
            elif pronostico.Local == pronostico.Visita:
                pronostico.Resultado = "Empate"
                pronostico.save()
        return render(request, "brasil/inicio.html")
    partidos_fifa = PartidosFifa.objects.order_by("Partido")[:64]
    context = {"partidos_fifa": partidos_fifa}
    return render(request, "brasil/inicio.html", context)

forms.py

class Pronosticos(ModelForm):
    class Meta:
        model = PartidosUsuarios
        fields = ["idPartido", "PaisL", "Local", "Visita", "PaisV"]

models.py

class PartidosUsuarios(models.Model):
    idUsuario = models.OneToOneField(User)
    idPartido = models.CharField(max_length=20)
    PaisL = models.CharField(max_length=250)
    Local = models.IntegerField(max_length=11, default=0)
    Visita = models.IntegerField(max_length=11, default=0)
    PaisV = models.CharField(max_length=250)
    Resultado = models.CharField(max_length=250)
    Puntos = models.IntegerField(max_length=11, default=0)
    Capturado = models.CharField(max_length=10, default="No")
    def __unicode__(self):
        return unicode(self.idPartido)

最后是表单和输入的html示例:

<tr>
                    <input type="hidden" value="1" name="idPartido1" id="idPartido"/>
                    <td><input value="Brasil" class="pronosticos" name="PaisL1" id="PaisL"/></td>
                    <td><input name="Local1" id="Local"/></td>
                    <td>-</td>
                    <td><input name="Visita1" id="Visita"/></td>
                    <td><input value="Croacia"  class="pronosticos" name="PaisV1" id="PaisV"/></td>
                </tr>
                <tr>
                    <input type="hidden" value="2" name="idPartido2" id="idPartido"/>
                    <td><input value="México" class="pronosticos" name="PaisL2" id="PaisL"/></td>
                    <td><input name="Local2" id="Local"/></td>
                    <td>-</td>
                    <td><input name="Visita2" id="Visita"/></td>
                    <td><input value="Camerún"  class="pronosticos" name="PaisV2" id="PaisV"/></td>
                </tr>

似乎它抱怨视图中的行以&#34; pronostico开头,_ =&#34;

非常感谢任何帮助。

EDIT 这是错误追溯:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/inicio/

Django Version: 1.6.2
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'brasil2014',
 'south')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/Users/eduardoalcocer/Desktop/PAGINAS WEB/Quinielas/brasil2014/views.py" in inicio
  84.             pronostico, _ = PartidosUsuarios.objects.get_or_create(idUsuario=request.user, idPartido=request.POST.get("idPartido"+str(i), ""), PaisL=request.POST.get("PaisL"+str(i), ""), Local=request.POST.get("Local"+str(i), ""), Visita=request.POST.get("Visita"+str(i), ""), PaisV=request.POST.get("PaisV"+str(i), ""), Capturado="Si")
File "/Library/Python/2.7/site-packages/django/db/models/manager.py" in get_or_create
  154.         return self.get_queryset().get_or_create(**kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in get_or_create
  373.             return self.get(**lookup), False
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in get
  298.         clone = self.filter(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in filter
  590.         return self._filter_or_exclude(False, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
  608.             clone.query.add_q(Q(*args, **kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in add_q
  1198.         clause = self._add_q(where_part, used_aliases)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in _add_q
  1234.                     current_negated=current_negated)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in build_filter
  1125.         clause.add(constraint, AND)
File "/Library/Python/2.7/site-packages/django/utils/tree.py" in add
  104.         data = self._prepare_data(data)
File "/Library/Python/2.7/site-packages/django/db/models/sql/where.py" in _prepare_data
  79.             value = obj.prepare(lookup_type, value)
File "/Library/Python/2.7/site-packages/django/db/models/sql/where.py" in prepare
  352.             return self.field.get_prep_lookup(lookup_type, value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
  1079.         return super(IntegerField, self).get_prep_lookup(lookup_type, value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
  369.             return self.get_prep_value(value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
  1073.         return int(value)

Exception Type: ValueError at /inicio/
Exception Value: invalid literal for int() with base 10: ''

1 个答案:

答案 0 :(得分:0)

出现错误是因为你正试图存储一个空字符串''是一个整数字段。

使用前检查表单值。