[Answer]-Django get urlpatterns regex by view name

1👍

The urls.py is like any other module where you can import it and see what things it has.

The below is quite crude but works and shows off the API.

import myapp.urls as my_urls

regex_pattern = my_urls.urlpatterns[2].regex.pattern
>> ^recruit/manage/(?P<id>[0-9]+)

a bit better

regex_object = filter(lambda x:'xxx_recruit_manage' == x.name, my_urls.urlpatterns)
>> [<RegexURLPattern recruit_manage ^recruit/manage/(?P<id>[0-9]+)$>]

Leave a comment