I have urls paths with name in my urls.py file.
urls.py
urlpatterns = [
path('home/', views.home, name="home_view"),
]
my views.py
def home(request):
path_name = get_path_name(request.path)
return HttpResponse(path_name)
Now, I need to get the path name, "home_view" in the HttpResponse.
How can I make my custom function, get_path_name() to return the path_name by taking request.path as argument?
You can use
request.resolver_match.view_nameto get thenameof current view.def home(request): path_name = request.resolver_match.view_name return HttpResponse(path_name)