Simple Django Debugger decorator

Here's a Python decorator that you can apply to a Django view function which will allow you to step through things and the code to help with debugging.

You can put a import pdb ; pdb.set_trace() inside the view function, and that'll do roughly the same thing, however with this approach, you can see other decorators and see what they are doing. This is helpful if you want to debug, or step through, inbuilt django decorators.

def debug_me(func):
    @wraps(func)
    def newfunc(*args, **kwargs):
        import pdb ; pdb.set_trace()
        return func(*args, **kwargs)
    return newfunc

Use it like this:

@debug_me
@cache_page
@ssl_required
def my_view(request, my_id):
    ...

This allows you to step through the @cache_page and @ssl_required decorator in case they are messing up with your view.

This entry is tagged: