【问题标题】:KeyError when python files executing in django application在 django 应用程序中执行 python 文件时出现 KeyError
【发布时间】:2012-05-30 20:55:47
【问题描述】:
TypeError at /python_tutor
'instancemethod' object is not subscriptable

当我在在线 python 导师脚本中运行此脚本时:

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.get["user_script",none]
        exec mycode

名称错误在 /python_tutor 名称“无”未定义

【问题讨论】:

  • 说真的,您正在执行用户上传的随机代码?你最好希望他们都不上传import os; os.system('rm -rf /')
  • @DanielRoseman 你知道那不会被执行:)

标签: django django-views python-2.7


【解决方案1】:

尝试将none 更改为None。 资本化问题。此外,get 之后似乎应该有圆括号而不是方括号。

【讨论】:

  • 感谢您的帮助。它的工作。但我想在同一页面上显示输出。它在终端给出......
  • 那是完全不同的事情。我建议您进一步阅读您正在使用的教程(我做对了吗?)。当您了解 Django 如何生成页面时,欢迎您回来提问,如果有的话:)
  • 我知道如何生成页面。 github.com/pgbovine/OnlinePythonTutor 。在此链接中:cgi-bin/web_exec.py / 请通过该链接并帮助我。我如何在我的 django 应用程序views.py 中调用 web-exec.py 。
  • 我的views.py代码是:from django.shortcuts import get_object_or_404, render_to_response from django.template import RequestContext import os defutor(request): print "Inisdeutor", request.method print "POST = ", request.POST if request.method == "POST" : print "Inside Post" print "My Script =", request.POST["user_script"] mycode = request.POST.get("user_script",None) exec我的代码返回 render_to_response('tutor.html', context_instance=RequestContext(request))
  • 您需要在“上下文”中将要显示的所有内容传递给render_to_response。我真的建议为 Django 专家创建一个新问题以查看它并提供帮助。
猜你喜欢
  • 1970-01-01
  • 2018-06-11
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 2020-11-21
  • 2020-08-28
相关资源
最近更新 更多