Issue
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?
Solution
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
Answered By - KillianDS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.