开发者

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

Sometimes the best way to debug s开发者_StackOverflowomething is to print some stuff to the page, and exit(), how can I do this in a Python/Django site?

e.g. in PHP:

echo $var;
exit();

Thanks


Put this in your view function:

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


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜