Django and Tastypie Reverse URL

We are moving our API from Django - Piston to Django-TastyPie. Everything went smoothly: "until we got to this:

urls.py applications

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

This uses a piston, so we want to change it to 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 are stuck in this error

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

And debugging the result:

Using the URLconf defined in MelodyService.urls, Django tried these URL patterns in the following 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 /, does not match any of them.

Anyone who knows the problem? This can be a really dumb / noobish question ...

+7
source share
3 answers

For future visitors who encounter this problem, the URL name 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 them in a callback, you can use them in your HTML like this:

 {% url "api_get_schema" resource_name="myresource" api_name="v1" %} 
+35
source

Django 'include' does not support names. Tastypie url names can be found at https://github.com/toastdriven/django-tastypie/blob/master/tastypie/resources.py : Resource.base_urls ()

+3
source

I can not write comments, so I have to post here to include it in my template, you have to do

 {% url "api_dispatch_list" resource_name="app_name" api_name='v1' %}?format=json 

or in my case it worked ONLY without the api part

 {% url "api_dispatch_list" resource_name="app_name" %}?format=json 

to get a list of available urls of your resource, import your resource from the python shell, then run the following command

 for url in ExampleResource().urls: print(url.name) 

you should get something like this

 api_dispatch_list api_get_schema api_get_multiple api_dispatch_detail 

For more information or if you are using a namespace check this out https://github.com/toastdriven/django-tastypie/issues/409

0
source

All Articles