【问题标题】:Print statement causing 500 response打印语句导致 500 响应
【发布时间】:2014-07-25 06:04:09
【问题描述】:
def gettable(request):
    reqdata = request.POST
    data = reqdata['dataabc']
    # print data
    return HttpResponse("OK")

这可行,但一旦我取消注释 print data,我就会在我的开发控制台中看到 500 响应。

为什么会发生这种情况?我只需要在控制台上打印一些东西来测试。

【问题讨论】:

  • filterlist 是什么意思(代码不完整)?
  • 对不起,我的意思是取消注释print data
  • 您可以添加来自开发控制台的 500 响应吗?可能会有所帮助。
  • 另外,你用的是什么版本的python?

标签: django python-3.x


【解决方案1】:

python 3

In python 3, print is a function. 使用print 作为语句将失败并引发异常,这将提前终止您的视图函数并导致错误500

日志记录模块

print 在库和服务器端/后台任务代码中是不好的做法。请改用logging 模块。 django 甚至有一个section for how to configure and use logging properly

mod_wsgi 和打印

https://code.google.com/p/modwsgi/wiki/DebuggingTechniques

mod_wsgi 3.0 版之前,您会在将printsys.stdout 一起使用时看到这一点:

IOError: sys.stdout access restricted by mod_wsgi

您需要明确使用文件,例如:

print >> sys.stderr, data  # python 2

但是,您可以通过编辑配置并使用 WSGIRestrictStdout 指令来禁用限制。

【讨论】:

    【解决方案2】:

    完全同意dnozay。日志模块是一种方法。

    有关使用 wsgi 的打印语句的更多信息: http://blog.dscpl.com.au/2009/04/wsgi-and-printing-to-standard-output.html?m=1

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多