访问request.POST时的KeyError

时间:2012-05-24 07:31:26

标签: python django django-forms

我在python中遇到了关键错误。

这样的错误..........

KeyError at /python_tutor/
'user_script'

并且代码是

import web_exec
def tutor(request):
    print "Inisde tutor", request.method
    print "POST =", request.POST
    if request.method == "POST" :
        print "Inside Post"
        print "My Script =", request.POST["user_script"]
        mycode = request.POST["user_script"]
        exec mycode 
    web_exec('mycode')

请给我解决方案吗?

2 个答案:

答案 0 :(得分:3)

mycode = request.POST.get('user_script', None)

答案 1 :(得分:1)

这意味着字典中不存在密钥(user_script)(request.POST);这表明它没有被提交。尝试:

if 'user_script' in request.POST:
    print "My Script =", request.POST["user_script"]
    mycode = request.POST["user_script"]