3

say this is my url:

url(r'^$', mainApp_views.homepage),

Is it possible to check this text = "mainApp_views.homepage" in view and middleware ?

Thanks in advance,

2 Answers 2

2

You want something like

from django.urls import resolve


def your_view(request):
    # resolve the url from the path
    url_name = resolve(request.path).url_name

You will probably want to add names to your urls too name='home'

Sign up to request clarification or add additional context in comments.

3 Comments

@python_beg2 lol not quite =) I had the same problem myself at one point =D
I think this should be from django.urls import resolve nowadays.
@TreyPiepmeier Possibly but this was 1.10/1.11 but thank you =)
0

As of Django 2.0, you need to import resolve from django.urls:

from django.urls import resolve

def your_view(request):
    url_name = resolve(request.path).url_name
    print(url_name) #prints the "name" attribute in your urlpatterns

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.