[英]django MultiValueDictKeyError error, how do I deal with it
[英]Error django “ MultiValueDictKeyError at /employeeUpdate/ 'id' ” affter I edit data
編輯數據后出現錯誤 django“MultiValueDictKeyError at /employeeUpdate/'id'”。 這是我在views.py中的代碼
def employeeUpdate(request):
id = request.POST['id']
emp_username = request.POST['emp_username']
emp_password = request.POST['emp_password']
emp_identification_code = request.POST['emp_identification_code']
content = employee.objects.get(pk = id)
content.emp_username = emp_username
content.emp_password = emp_password
content.emp_identification_code = emp_identification_code
if(employee.objects.filter(emp_username=emp_username)):
messages.success(request, 'Username already exists')
return redirect("/employeeUpdate")
elif(employee.objects.filter(emp_identification_code=emp_identification_code)):
messages.success(request, 'Identification already exists')
return redirect("/employeeUpdate")
else:
content.save()
messages.success(request, 'Update already !!!')
return redirect("/employeeBoard")
您正在嘗試訪問不存在request.POST['id']
。 如果字典不存在,您可以嘗試從字典中訪問默認值
id =request.POST.get('id', default_value)
如果您想訪問當前用戶的id
,那么您可以嘗試
id = request.user.id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.