DEV Community

Cover image for ValueError Occurring During Saving the Form

ValueError Occurring During Saving the Form

Shailesh Waghole on April 03, 2020

After click on Submit button Value error is occur. I got this error: Field 'id' expected a number but got 'Shailesh'. Request Method: POST Request ...
Collapse
 
soumyaranjannaik profile image
Soumya Ranjan Naik

Looks like you are assigning some string to id which should be a autofield.
Show the view where you create and save the object then I can assist you further.

Collapse
 
shailesh6363 profile image
Shailesh Waghole

Hello Soumya,
Following is my views.py:

from django.shortcuts import render
from home.models import Contact

Create your views here.

def index(request):

return render(request,'index.html')

def about(request):
return render(request,'about.html')

def services(request):
return render(request,'services.html')

def contact(request):
if request.method =="POST":
name=request.POST.get('name')
phone=request.POST.get('phone')
email=request.POST.get('email')
desc=request.POST.get('desc')
obj1=Contact(name,phone,email,desc)
obj1.save()

return render(request,'contact.html')

def str(self):
self.name

Collapse
 
soumyaranjannaik profile image
Soumya Ranjan Naik

Where you create obj1 there instead of creating objects using positional arguments use keyword arguments.
So change line will look like

obj1=Contact(name=name,phone=phone,email=email,desc=desc)

The keywords should be the name of the columns in the model

Thread Thread
 
shailesh6363 profile image
Shailesh Waghole

Hello Soumya,
After changing it's working fine. Thanks for my correction. Please reply me back with your contact details. I want to add you to my contact.

Thread Thread
 
soumyaranjannaik profile image
Soumya Ranjan Naik • Edited

You can contact me on telegram on
t.me/soumya_r

 
shailesh6363 profile image
Shailesh Waghole

Hello Pavel,
I tried this method and now it is working fine. Thanks for my correction. I will use code blocks in my post. Thanks again. Please reply to me with your contact details. I want to add you to my contact.

Collapse
 
shailesh6363 profile image
Shailesh Waghole

Hello Pavel,
Following is my views.py:

from django.shortcuts import render
from home.models import Contact

Create your views here.

def index(request):

return render(request,'index.html')

def about(request):
return render(request,'about.html')

def services(request):
return render(request,'services.html')

def contact(request):
if request.method =="POST":
name=request.POST.get('name')
phone=request.POST.get('phone')
email=request.POST.get('email')
desc=request.POST.get('desc')
obj1=Contact(name,phone,email,desc)
obj1.save()

return render(request,'contact.html')

def str(self):
self.name

 
shailesh6363 profile image
Shailesh Waghole

Okay. No Issue. Again Thanks for Correction