Is it possible to get the name of Django view in string form? I have searched a lot but could not find the answer.
Asked
Active
Viewed 3,605 times
4
-
There isn't anything built-in to do that. You can get the current app, but you'd have to write a context processor or something to get the view. – markwalker_ Oct 28 '17 at 12:46
-
1Possible duplicate of [Django: How can I identify the calling view from a template?](https://stackoverflow.com/questions/792410/django-how-can-i-identify-the-calling-view-from-a-template) – souldeux Oct 28 '17 at 12:52
-
1`request.resolver_match.func.__name__` inside a view could work but you should tell why you need it, maybe there is a better pattern – PRMoureu Oct 28 '17 at 12:53
-
Thank you very much @PRMoureu , it worked like a charm. – tayyab_fareed Oct 30 '17 at 05:14
-
What about, `self.get_view_name()` ? (in **CBV**) – JPG Mar 08 '18 at 08:21
3 Answers
2
The above did not work for me, but I was able to find some solutions by running dir
on request
.
request.resolver_match.view_name
seems to give me the correct name for the view.

Navid Khan
- 979
- 11
- 24
0
If you get view as a function from resolve
you can check it like this:
view, _, _ = resolve(request.path)
view_name = view.__name__

muradin
- 1,249
- 2
- 14
- 34