1

I am getting the below error while validating the field using Javascript and Django.

Error:

ValueError at /insert/
invalid literal for int() with base 10: ''
Request Method: POST
Request URL:    http://127.0.0.1:8000/insert/
Django Version: 1.11.2
Exception Type: ValueError
Exception Value:    
invalid literal for int() with base 10: ''
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py in get_prep_value, line 1853
Python Executable:  /usr/bin/python
Python Version: 2.7.6

I am explaining my code below.

insert.html:

<script type="text/javascript">
    function validateForm(){
        var s=document.frmfeed;
        if(s.name==''){
            alert('Please enter the name');
            return;
        }else if(s.phone==''){
            alert('Please enter the phone no');
            return;
        }else if(s.age==''){
            alert('Please enter the age');
            return;
        }else{

        }
    }
</script>

<form method="post" action=" " onSubmit="return validateForm();" name="frmfeed">
    {% csrf_token %}
    <label>Name: </label>
    <input name="name" value="{{person.name}}">
    <br>
    <label>Phone: </label>
    <input name="phone" value="{{person.phone}}">
    <br>
    <label>Age: </label>
    <input name="age" value="{{person.age}}">
    <br>
    <input type="hidden" name="id" value="{{person.id}}">
    <input type="submit" value="Submit">
</form>

view.py:

def insert(request): 
    # If this is a post request we insert the person 
    if request.method == 'POST': 
        p = Person( 
        name=request.POST['name'], 
        phone=request.POST['phone'], 
        age=request.POST['age'] 
        ) 
        p.save() 
        return redirect('/') 
    else: 
        return render(request, 'insert.html', {})

model.py:

class Person(models.Model):
    name = models.CharField(max_length=200)
    phone = models.CharField(max_length=15)
    age = models.IntegerField()

Here I need to check all input fields those should not be blank before submit but when I am clicking on submit button those error are coming. Please help me to resolve this error.

7
  • can you add your insert view ? Commented Jun 13, 2017 at 13:40
  • I added the insert view. Commented Jun 13, 2017 at 13:42
  • invalid literal for int() with base 10: '' means that your code is trying to convert a string into an integer but you got an empty string that'll not be converted into an int and will throw this exception. i bet the problem is in your Person class. Commented Jun 13, 2017 at 13:45
  • 1
    age=int(request.POST['age']) if it's an InterfField Commented Jun 13, 2017 at 13:45
  • @subhra please add your Person class model Commented Jun 13, 2017 at 13:52

2 Answers 2

1

Your JS is not actually doing any validation, in that it is not preventing submission if the values are blank. You need to return false in those cases.

However, you should not do any of this. Modern browsers include form validation already, which will kick in if you add a required attribute on the fields.

Django itself will output that attribute - and, just as important, also validate on the server side - if you use a form class. You should definitely be doing this.

Sign up to request clarification or add additional context in comments.

3 Comments

Right now I am doing blank field check but later I might go for different type validation so in that case how can i handle it.
Again, HTML5 field validation will handle that, and Django will output the correct attributes if you use a ModelForm class.
Can you share your idea for my form. any one example is enough to get basic idea.
0

Your model contains an Integer field for age, you are passing a string to the field. request.POST['age'] always returns a string.

Try your view edited like this,

def insert(request): 
    if request.method == 'POST': 
        p = Person( name=request.POST['name'], phone=int(request.POST['phone']), age=int(request.POST['age']) )
        p.save() 
        return redirect('/') 
    else: 
        return render(request, 'insert.html', {})

Here I need to check all input fields those should

NOTE: Assuming yourphone field is also an Integer field. If not ignore the changes in the phone= field.

EDIT

You could specify type= "number" , in input tag of age in the form.

<form method="post" action=" " onSubmit="return validateForm();" name="frmfeed"> 
{% csrf_token %} 
<label>Name: </label> 
<input name="name" value="{{person.name}}" required> <br> 
<label>Phone: </label>
<input name="phone" value="{{person.phone}}" required> <br> 
<label>Age: </label> 
<input type="number" name="age" value="{{person.age}}" required> <br> 
<input type="hidden" name="id" value="{{person.id}}"> 
<input type="submit" value="Submit"> 
</form>

7 Comments

How do you know that phone and age are integers in his model ?
Inspect the error, it's clear.. and who would define a charfield for age??
Well it's true that the error is due to a bad convertion of string to an int. But personnaly i cannot answer untill i see his Person class model.
I did like age=int(request.POST['age']) this but still same error.
No, this is not the problem. Django will convert this as necessary - in fact the error itself shows that it is calling int internally. The problem, as also shown in that error, is that the value being sent is the empty string.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.