1👍
✅
I had to do this recently writing ExceptBot. That links to the source repo.
The relevant code is:
import traceback
class ExceptBotMiddleware:
def process_exception(self, request, exception):
# Get the current stack trace
current_traceback = traceback.extract_stack()
stack = current_traceback[-1]
# getting the line number
line_number = stack.lineno
Source:stackexchange.com