程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Fieldidexpected a number but got when modifying information in Python Django

編輯:Python
Error when modifying employee information Field 'id' expected a number but got ''.

views Function code in :

def edit_employee_old(request,emp_id): if request.method=="POST": id = request.POST.get('id') name = request.POST.get('name') email = request.POST.get('email') dep = request.POST.get('dep') info = request.POST.get('info') groups = request.POST.getlist('group') emp = employee.objects.get(id = id) emp.name = name emp.email = email emp.dep_id = dep emp.info_id = info emp.group.set(groups) emp.save() return redirect('/test_orm_old/list_employee_old/') emp = employee.objects.get(id = emp_id) dep_list = department.objects.all() group_list = group.objects.all() info_list = employeeinfo.objects.all() return render(request,'test_orm_old/edit_employee_old.html',{
'emp':emp,'dep_list':dep_list,'group_list':group_list,'info_list':info_list})

html Code :

<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <title> modify </title> </head> <body> <div align="center"> <h1> Modify employee information </h1> <hr> <form action="" method="post"> {% csrf_token %} <input type="hidden" name="id" id="id" value="{
{ emp_id }}"> <div> <label> full name :</label> <input type="text" name="name" id="name" value="{
{ emp_name }}"> </div> <br> <div> <label> mailbox :</label> <input type="text" name="email" id="email" value="{
{ emp_email }}"> </div> <br> <div> <label> Contact information :</label> <!-- For <select> In the tag <option>, from for Loop out the relevant values assigned to him value attribute --> <select name="info" id="info"> {% for info in info_list %} <!-- emp.info_id And info.id identical , It indicates that the employee one-to-one key field value is the same as the primary key value of the Employee Supplementary information record , Set up <option> The label is selected --> {% if emp.info_id == info_id %} <option value="{
{ info.id }}" selected> {
{ info.phone }}||{
{ info.address }} </option> {% else %} <option value="{
{ info.id }}" selected> {
{ info.phone }}||{
{ info.address }} </option> {% endif %} {% endfor %} </select> </div> <br> <div> <label> department :</label> <select name="dep" id="dep"> {% for dep in dep_list %} {% if emp.dep_id == dep.id %} <option value="{
{ dep.id }}" selected>{
{ dep.dep_name }}</option> {% else %} <option value="{
{ dep.id }}" selected>{
{ dep.dep_name }}</option> {% endif %} {% endfor %} </select> </div> <br> <div> <label> group :</label> <!-- Field group It's many to many , Set up <select> Tag attributes multiple="true" --> <select name="group" id="group" multiple="true"> {% for group in group_list %} <!-- If the group group Record objects in employee The current record is in the collection of many to many key Association community record objects , Set up current <option> The label is selected --> {% if group in emp.group.all %} <option value="{
{group.id}}" selected>{
{ group.group_name }}</option> {% else %} <option value="{
{group.id}}" selected>{
{ group.group_name }}</option> {% endif %} {% endfor %} </select> </div> <br> <div><input type="submit" value=" preservation "></div> </form> </div> </body></html>
Operation results and error reporting contents




Take the answer :

It's this line that goes wrong ?

emp = employee.objects.get(id = id)

See Tips , Seems to be because employee Of id yes int type , You do get In operation , Passed an empty string to Conditions
In terms of id , It is recommended to transfer first int

emp = employee.objects.get(id = int(id))

I suggest you check first Page html Code , have a look input Inside value If there is a value

<input type="hidden" name="id" id="id" value="{
{ emp_id }}">

without , Check that you call edit_employee_old The location of , Whether the transmission is correct emp_id



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved