开发者

Get view function from request uri?

Given a uri like /home/ I want to find the view function that this corresponds to, preferably in a f开发者_运维问答orm like app.views.home or just <app_label>.<view_func>. Is there a function that will give me this?


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.urls import resolve

myfunc, myargs, mykwargs = resolve("/hello_world/")
mymodule = myfunc.__module__

In case one needs the class of the view since a class based view is being used one can access the view_class of the returned function:

view_class = myfunc.view_class


From Django 2.0 onward django.core.urlresolvers module has been moved to django.urls.

You will need to do this:

from django.urls import resolve

myfunc, myargs, mykwargs = resolve("/hello_world/")
mymodule = myfunc.__module__


Since Django 1.3 (March 2011) the resolve function in the urlresolvers module returns a ResolverMatch object. Which provides access to all attributes of the resolved URL match, including the view callable path.

>>> from django.core.urlresolvers import resolve
>>> match = resolve('/')
>>> match.func
<function apps.core.views.HomeView>
>>> match._func_path
'apps.core.views.HomeView'


1. Generate a text file with all URLs with corresponding view functions

./manage.py show_urls --format pretty-json --settings=<path-to-settings> > urls.txt

example ./manage.py show_urls --format pretty-json --settings=settings2.testing > urls.txt

2. Look for your URL in the output file urls.txt

    {
        "url": "/v3/affiliate/commission/",
        "module": "api.views.affiliate.CommissionView",
        "name": "affiliate-commission",
    },


All the others focus on just the module or string representation of the view. However, if you want to directly access the view object for some reason, this could be handy

resolve('the_path/').func.cls

This gives the view object itself, this works on class based view, I haven't tested it on a function based view though.


Based on KillianDS's answer, here's my solution:

from django.core.urlresolvers import resolve

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜