NameError - 未定义全局名称

时间:2011-02-23 11:37:23

标签: python django django-models django-templates django-views

我正在尝试从html表单中读取一些数据并将其插入到数据库中。这样做我坚持这个错误“/ nameEmployee /的NameError:全局名称'get_post_param'未定义”;我会在这里粘贴我的代码。有人可以帮我解决这个问题 VIEWS.PY

def createEmployee(request):
    if request.method == "POST":
        userName = get_post_param(request,"userName")
        designation = get_post_param(request,"designation")
        employeeID = get_post_param(request,"employeeID")
        contactNumber = get_post_param(request,"contactNumber")
        project = get_post_param(request,"project")
        dateOfJoin = get_post_param(request,"dateOfJoin")
        EmployeeDetails(userName=userName,designation=designation,employeeID=employeeID,contactNumber=contactNumber,project=project,dateOfJoin=dateOfJoin).save()
        return render_to_response('createEmployee.html')
    else:
        return render_to_response('createEmployee.html')

TEMPLATE.PY

<form action="http://127.0.0.1:8000/createEmployee/" method="POST"> 
Name: <input type="text" name="userName" /><br />
Designation: <input type="text" name="designation" /><br>
EmployeeID: <input type="text" name="employeeID" /><br>
Contact Number: <input type="text" name="contactNumber" /><br>
Project: <input type="text" name="project" /><br>
Date Of Join: <input type="text" name="dateOfJoin" /><br>
<input type="submit" value="Submit" /><br />
</form>

1 个答案:

答案 0 :(得分:5)

这是一个非常简单的问题。您尚未定义get_post_param 你在哪里得到这个get_post_param的想法?

只需定义一个名为get_post_param的函数来修复它。

这是一个python shell会话

>>> get_post_param(request, 'userName')
NameError: not defined

>>> def get_post_param(request, param):
...    return request.POST.get(param) 
       # or scrap the idea of a function to do a one liner op anyways

>>> get_post_param(request, 'userName'):
'my_username'