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


I am trying out some stuff in 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.

I have a view like this.

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

        #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)
            else :
                return users[0].id 
        return 0

My file contains :

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

My contains this :

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/", line 39, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/generic/", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/generic/", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/akshay/Downloads/mindwave/dashboard/", line 27, in post
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 796, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", 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/", line 889, in _save_table
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 919, in _do_update
    filtered = base_qs.filter(pk=pk_val)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 796, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", 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/", 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/", 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/", line 1187, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/", line 1083, in build_lookup
    return final_lookup(lhs, rhs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 19, in __init__
    self.rhs = self.get_prep_lookup()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", 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/", 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.

Show source
| python   | django   2017-01-04 13:01 1 Answers

Answers to Django: ValueError: invalid literal for int() with base 10: ( 1 )

  1. 2017-01-04 13:01

    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.

    Solution. Never initialize models like this. Always pass named parameters

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

Leave a reply to - Django: ValueError: invalid literal for int() with base 10:

◀ Go back