Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I have urls paths with name in my urls.py file.

urls.py

urlpatterns = [
    path('home/', views.home, name="home_view"),
]

my views.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading