Following is the views.py and on runserver getting error as the traceback attached below.
def Create(request):
form = ListForm()
if request.method == 'GET':
context = {'form': form, }
return render(request, 'CRUD/add.html', context)
if request.method == 'POST':
form = ListForm(request.POST)
if form.is_valid():
ListFormData = form.cleaned_data
ListFormDataForSave = List(
Donid = ListFormData['Donid'],
Name = ListFormData['Name'],
addr1 = ListFormData['addr1'] ,
addr2 = ListFormData['addr2'],
addr3 = ListFormData['addr3'],
City = ListFormData['City'],
State = ListFormData['State'],
Pincode = ListFormData['Pincode'],
PAN = ListFormData['PAN'],
Aadhar = ListFormData['Aadhar'],
Phone = ListFormData['Phone'],
Mobile = ListFormData['Mobile'],
Email = ListFormData['Email'],
Remark = ListFormData['Remark']
)
ListFormDataForSave.Save()
return redirect('list')
context = {'form': form, }
return render(request, 'CRUD/add.html', context)
Traceback
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/CRUD/add/
Django Version: 4.0.5
Python Version: 3.10.4
Installed Applications:
['app',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_tables2',
'django_filters',
'django_htmx',
'crispy_forms',
'DTables2',
'Apps.Dapp.apps.DappConfig'
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_htmx.middleware.HtmxMiddleware']
Traceback (most recent call last):
File "F:\Program Files\Python310\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "F:\Program Files\Python310\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\IT\Projects\Django\DjangoWebProject\Apps\Dapp\views.py", line 31, in Create
Donid = ListFormData['Donid'],
Exception Type: KeyError at add/
Exception Value: 'Donid'