user1265125
user1265125

Reputation: 2656

Print statement causing 500 response

def gettable(request):
    reqdata = request.POST
    data = reqdata['dataabc']
    # print data
    return HttpResponse("OK")

This works, but as soon as I uncomment print data, I see a 500 response in my dev console.

Why could this be happening? I just need to print a couple of things to the console to test.

Upvotes: 0

Views: 102

Answers (2)

dnozay
dnozay

Reputation: 24344

python 3

In python 3, print is a function. Using print as a statement will fail and raise an exception which will terminate your view function prematurely and cause an error 500.

logging module

print is bad practice in libraries and in server-side / background tasks code. please use the logging module instead. django even has a section for how to configure and use logging properly.

mod_wsgi and print

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

Prior to mod_wsgi version 3.0, you would see this when using print with sys.stdout:

IOError: sys.stdout access restricted by mod_wsgi

and you would need to explicitly use a file, e.g.:

print >> sys.stderr, data  # python 2

You can however disable the restriction by editing your configuration and using the WSGIRestrictStdout directive.

Upvotes: 2

DeeGautam
DeeGautam

Reputation: 41

Completely agree with dnozay. Logging module is a way to go.

More about print statements with wsgi: http://blog.dscpl.com.au/2009/04/wsgi-and-printing-to-standard-output.html?m=1

Upvotes: 0

Related Questions