简体   繁体   English

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

[英]Django: ValueError: invalid literal for int() with base 10:

I am trying out some stuff in django. 我在django尝试一些东西。 Just a simple form which would add every attempt to login in a database. 只是一个简单的表单,它将添加每次登录数据库的尝试。 It results in ValueError: invalid literal for int() with base 10: I had checked out a lot of questions but wasn't able to get past this error. 它导致ValueError:对于带有基数10的int()的无效文字:我检查了很多问题但是无法通过此错误。

I have a view like this. 我有这样的看法。

class LoginView(TemplateView):
    template_name = 'dashboard/login.html'
    def post(self,request):

        #firstname,email=request.POST['firstname'],request.POST['email']
        #currLogin = LoginForm(firstname,email)
        currLogin = LoginForm(request.POST)
        if currLogin.is_valid():
            firstname= currLogin.cleaned_data['firstname']
            email = currLogin.cleaned_data['email']
            print firstname,email
            users = LoginModel.objects.filter(email=email)

            if not users:
                login = LoginModel(firstname,email)
                login.save()
            else :
                return users[0].id 
        return 0

My models.py file contains : 我的models.py文件包含:

class LoginModel(models.Model):
    firstname = models.CharField(max_length=100)
    email = models.CharField(max_length=100)

My form.py contains this : 我的form.py包含这个:

class LoginForm(forms.Form):
    firstname = forms.CharField(label="firstname",max_length=100)
    email = forms.CharField(label="email",max_length=100)
    class Meta:
        model = LoginModel

Stack Trace : 堆栈跟踪 :

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/akshay/Downloads/mindwave/dashboard/views.py", line 27, in post
    login.save()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 796, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 824, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 889, in _save_table
    forced_update)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 919, in _do_update
    filtered = base_qs.filter(pk=pk_val)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 796, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 814, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1227, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1253, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1187, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1083, in build_lookup
    return final_lookup(lhs, rhs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/lookups.py", line 19, in __init__
    self.rhs = self.get_prep_lookup()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/lookups.py", line 59, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 946, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'Ak

Any Help in figuring this out is appreciated. 任何有助于解决这个问题的帮助表示赞赏。

This is the culprit. 这是罪魁祸首。

 login = LoginModel(firstname, email)

When you initialize a model in this way, the arguments are passed to the fields in the order in which they (the fields) are defined. 以这种方式初始化模型时,参数将按照定义它们(字段)的顺序传递给字段。 Now your first argument is firstname and that will be assigned to the primary key field which is defined for your model automatically by Django and is considered to be the first. 现在你的第一个参数是firstname ,它将被分配给Django自动为你的模型定义的主键字段,并被认为是第一个。

Solution. 解。 Never initialize models like this. 永远不要像这样初始化模型。 Always pass named parameters 始终传递命名参数

login = LoginModel(firstname=firstname, email=email)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django ValueError:以10为底的int()的无效文字: - django ValueError: invalid literal for int() with base 10: Django:ValueError:以10为底的int()的无效文字 - Django:ValueError: invalid literal for int() with base 10 Django:ValueError以10为底的int()无效文字 - Django: ValueError invalid literal for int() with base 10 django ValueError:以10为底的int()的无效文字:'' - django ValueError: invalid literal for int() with base 10: '' ValueError: int() 以 10 为底的无效文字:'' | Django - ValueError: invalid literal for int() with base 10: '' | Django ValueError:ind()的无效文字(以django.setup()为基数为10) - ValueError: invalid literal for int() with base 10 with django.setup() ValueError-int()以10为底的无效文字:由Django URL引起的“添加” - ValueError - Invalid literal for int() with base 10: 'add' caused by Django URLs Django 1.6:ValueError以10为底的int()无效文字 - Django 1.6: ValueError invalid literal for int() with base 10 Django,Postgres:ValueError:int() 的无效文字,基数为 10:' ' - Django , Postgres : ValueError: invalid literal for int() with base 10: ' ' Django 1.7 ValueError:以10为基数的int()的无效文字:'a' - Django 1.7 ValueError: invalid literal for int() with base 10: 'a'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM