Home > Blockchain >  How to get the path name of an URL path in django using the request.path
How to get the path name of an URL path in django using the request.path

Time:06-23

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?

CodePudding user response:

You can use request.resolver_match.view_name to get the name of current view.

def home(request):
    path_name = request.resolver_match.view_name
    return HttpResponse(path_name)
  • Related