Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch any exception in python and print stack trace (DJANGO)

Tags:

python

django

I have like this:

try:
    #bunch of code
except:
    return HttpResponse....

I want to send an error to client and to print stack trace on console. How can i do that?

like image 788
milandjukic88 Avatar asked Dec 07 '22 03:12

milandjukic88


1 Answers

You can do something like this

import traceback 
from django.http import HttpReponse

def view(request):
     try:
          #throw exception
     except:
         tb = traceback.format_exc()
         return HttpResponse(tb)
    # normal flow
like image 168
Serafeim Avatar answered May 13 '23 16:05

Serafeim