Akshay Hazari
Akshay Hazari

Reputation: 3267

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):

        #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 :

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

My form.py 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/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.

Upvotes: 3

Views: 2146

Answers (1)

e4c5
e4c5

Reputation: 53734

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)

Upvotes: 10

Related Questions