Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse URL problem with Django and Tastypie

We're porting our API from Django - Piston to Django-TastyPie. Everything went smoothly, 'till we got to this:

urls.py of the app

 url(r'^upload/', Resource(UploadHandler, authentication=KeyAuthentication()), name="api-upload"),
    url(r'^result/(?P<uuid>[^//]+)/', Resource(ResultsHandler, authentication=KeyAuthentication()), name="api-result")

This uses piston, so we want to change it into something for tastyPie

url(r'^upload/', include(upload_handler.urls), name="api-upload"),
url(r'^result/(?P<uuid>[^//]+)/', include(results_handler.urls), name="api-result")

But we're stuck on this fault

Reverse for 'api-result' with arguments '()' and keyword arguments '{'uuid': 'fbe7f421-b911-11e0-b721-001f5bf19720'}' not found.

And the Debugpage of result:

Using the URLconf defined in MelodyService.urls, Django tried these URL patterns, in this order:

^melotranscript/ ^upload/ ^melotranscript/ ^result/(?P[^//]+)/ ^(?Presultshandler)/$ [name='api_dispatch_list'] ^melotranscript/ ^result/(?P[^//]+)/ ^(?Presultshandler)/schema/$ [name='api_get_schema'] ^melotranscript/ ^result/(?P[^//]+)/ ^(?Presultshandler)/set/(?P\w[\w/;-]*)/$ [name='api_get_multiple'] ^melotranscript/ ^result/(?P[^//]+)/ ^(?Presultshandler)/(?P\w[\w/-]*)/$ [name='api_dispatch_detail'] ^melotranscript/ ^processed/(?P.)$ ^admin/doc/ ^TOU/$ [name='TOU'] ^$ [name='index'] ^admin/ ^doc/(?P.)$ The current URL, melotranscript/result/fbe7f421-b911-11e0-b721-001f5bf19720/, didn't match any of these.

Someone who knows the problem? It's maybe a really silly/noobish question...

like image 807
Lennart- Avatar asked Jul 28 '11 12:07

Lennart-


2 Answers

For future visitors who are having this problem, the name of the URL is api_dispatch_list, and you also need to specify the API name:

url = reverse('api_dispatch_list', kwargs={'resource_name': 'myresource', 'api_name': 'v1'})

There are other URL names that Tastypie also provides:

/schema/   -->  api_get_schema
/set/      -->  api_get_multiple
/$your-id/ -->  api_dispatch_detail

You can use those in a call to reverse, you can use them in your HTML like so:

{% url "api_get_schema" resource_name="myresource" api_name="v1" %}
like image 174
Mike Ryan Avatar answered Oct 04 '22 11:10

Mike Ryan


Django 'include' doesn't support names. Names of Tastypie urls you can find in https://github.com/toastdriven/django-tastypie/blob/master/tastypie/resources.py : Resource.base_urls()

like image 44
Glader Avatar answered Oct 04 '22 09:10

Glader