[Django]-Equivalent of PHP "echo something; exit();" with Python/Django?

17👍

Put this in your view function:

from django.http import HttpResponse
return HttpResponse(str(var))

14👍

I just wanted to give an alternative answer: Simply use print statements and serve your django site with python manage.py runserver

In this case the print statements show up in your shell, and your site continues functioning as it would normally.

3👍

If you are using mod_wsgi you can say:

print var
raise SystemExit

The SystemExit exception will not actually cause the whole process to exit as it would normally, but only cause that request to exit, presuming that no higher code catches it and ignores.

Ensure you are using mod_wsgi 3.X. If using older mod_wsgi, you would need to instead say:

import sys
print >> sys.stderr
raise SystemExit

Other WSGI servers may also treat SystemExit in a request the same way, say you would need to experiment as to what happens if using other hosting solution.

For other information about debugging WSGI applications read:

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

-1👍

If you don’t want to crash mod_wsgi on print’s do this in your wsgi file

sys.stdout = sys.stderr

prints will then be sent to the error_log

👤Mike

Leave a comment