-1

我在 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')

请给我解决方案?

4

2 回答 2

3

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

于 2012-05-24T08:16:17.513 回答
2

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

if 'user_script' in request.POST:
    print "My Script =", request.POST["user_script"]
    mycode = request.POST["user_script"]
于 2012-05-24T07:37:42.873 回答