tags:

views:

20

answers:

1

Given a uri like /home/ I want to find the view function that this corresponds to, preferably in a form like app.views.home or just <app_label>.<view_func>. Is there a function that will give me this?


Excellent! This is what I built out of it:

def response(request, template=None, vars={}):
    if template is None:
        view_func = resolve(request.META['REQUEST_URI'])[0]
        app_label = view_func.__module__.rsplit('.', 1)[1]
        view_name = view_func.__name__
        template = '%s.html' % os.path.join(app_label, view_name)
    return render_to_response(template, vars, context_instance=RequestContext(request))

Now you can just call return response(request) at the end of your view funcs and it will automatically load up app/view.html as the template and pass in the request context.

+2  A: 

You can use the resolve method provided by django to get the function. You can use the __module__ attribute of the function returned to get the app label. This will return a string like project.app.views. So something like this:

from django.core.urlresolvers import resolve

myfunc, myargs, mykwargs = resolve("/hello_world/")
mymodule = myfunc.__module__
KillianDS
Did the trick. Thanks :) Figured you guys could answer me faster than I could dig through the source ;)
Mark