Search code examples
pythondjangotastypie

How to redirect to the API's entry point without hardcoding the path?


I've tried calling reverse with the install of the Api to no avail. Using the shell to determine the name of the RegexpURLPattern I determine that the name was api_v2_top_level but calling reverse('api_v2_top_level_') doesn't work. Any ideas?

# How I determined the name of the view.
In [3]: v2_api.urls
Out[3]:
[<RegexURLPattern api_v2_top_level ^(?P<api_name>v2)/$>,
 <RegexURLResolver <RegexURLPattern list> (None:None) ^(?P<api_name>v2)/>,
 <RegexURLResolver <RegexURLPattern list> (None:None) ^(?P<api_name>v2)/>,
 <RegexURLResolver <RegexURLPattern list> (None:None) ^(?P<api_name>v2)/>]

from tastypie.api import Api
v2_api = Api(api_name='v2')
v2_api.register(...)
...
from api.urls import v2_api
In [4]: v2_api.urls[0]
Out[4]: <RegexURLPattern api_v2_top_level ^(?P<api_name>v2)/$>

In [5]: v2_api.urls[0].name
Out[5]: u'api_v2_top_level'

Solution

  • reverse('api_v2_top_level', kwargs={'api_name': 'v2'}) should do the trick.