1

Is there an inverse method for Django's django.core.urlresolvers.reverse()?

I want a function f(x) such that f(django.core.urlresolvers.reverse('shadowfax')) == 'shadowfax'.

Does anybody have anything to say on whether this would be a proper approach to writing unit tests for methods that call .reverse()?

Dan Hoerst
  • 6,222
  • 2
  • 38
  • 51
foobar
  • 25
  • 4

2 Answers2

2

Probably shocking:

django.core.urlresolvers.resolve( "/test" ).url_name

It is perfectly good way to use it and it is even recommend in the documentation:

https://docs.djangoproject.com/en/dev/ref/urlresolvers/

freakish
  • 54,167
  • 9
  • 132
  • 169
1

An updated version.

from django.urls import resolve
resolve( "/test" ).url_name
MikeSchem
  • 950
  • 2
  • 16
  • 29