Get view function from request uri?

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__

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

Tags:

Django