views:

64

answers:

1

For debugging purposes, I'd like a quick way (e.g. in manage.py shell) of looking up which view that will be called as a result of a specific URL being requested.
I know this is what django.core.urlresolvers.resolve does, but when having a decorator on the view function it will return that decorator.
Example:

>>>django.core.urlresolvers.resolve('/edit_settings/'))
(Allow, (), {})

...where Allow is the decorator, not the view it's decorating.

How can I find the view without manually inspecting the urls.py files?

+1  A: 

This isn't my area of expertise, but it might help.

You might be able to introspect Allow to find out which object it's decorating.

>>>from django.core.urlresolvers import resolve
>>>func, args, kwargs=resolve('/edit_settings/')
>>>func
Allow

You could try

>>>func.func_name

but it might not return the view function you want.

Here's what I found when I was experimenting with basic decorator functions:

>>>def decorator(func):
...    def wrapped(*args,**kwargs):
...        return func(*args,**kwargs)
...    wrapped.__doc__ = "Wrapped function: %s" %func.__name__
...    return wrapped

>>>def add(a,b):
...    return(a,b)

>>>decorated_add=decorator(add)

In this case, when I tried decorated_add.func_name it returned wrapped. However, I wanted to find a way to return add. Because I added the doc string to wrapped, I could determine the original function name:

>>>decorated_add.func_name
wrapped
>>>decorated_add.__doc__
'Wrapped function: add'

Hopefully, you can find out how to introspect Allow to find out the name of the view function, possibly by modifying the decorator function.

Alasdair