开发者

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...


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" %}


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()


I can't write comments so I have to post here to include it in your template you should 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 list of available urls of your resource, import your resource from python shell then execute 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 details or if you are using namespace check this https://github.com/toastdriven/django-tastypie/issues/409

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜