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.
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 yourPersonclass.Personclass model