Django和Tastypie的反向URL问题

时间:2011-07-28 12:09:01

标签: django django-piston tastypie

我们正在将我们的API从Django - Piston移植到Django-TastyPie。一切顺利,'直到我们达到这个目的:

应用的urls.py

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

这使用活塞,所以我们想把它变成适合TastyPie的东西

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

但我们坚持这个错误

  

使用参数'()'和关键字参数'{'uuid':'fbe7f421-b911-11e0-b721-001f5bf19720'}'找不到'api-result'。

结果的调试页面:

  

使用MelodyService.urls中定义的URLconf,Django按以下顺序尝试了这些URL模式:

^ 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 / ^已处理/(?P )$ ^管理员/ DOC / ^ TOU / $ [name ='TOU'] ^ $ [name ='index'] ^管理员/ ^ DOC /(ΔP)$ 当前的URL,melotranscript / result / fbe7f421-b911-11e0-b721-001f5bf19720 /,与这些中的任何一个都不匹配。

有谁知道这个问题?这可能是一个非常愚蠢/愚蠢的问题......

3 个答案:

答案 0 :(得分:35)

对于遇到此问题的未来访问者,网址名称为api_dispatch_list,您还需要指定API名称:

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

还有其他网址that Tastypie also provides

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

您可以在通话中使用这些内容进行反转,您可以在HTML中使用它们,如下所示:

{% url "api_get_schema" resource_name="myresource" api_name="v1" %}

答案 1 :(得分:3)

Django'include'不支持名称。您可以在https://github.com/toastdriven/django-tastypie/blob/master/tastypie/resources.py找到的Tastypie网址名称:Resource.base_urls()

答案 2 :(得分:0)

我不能写评论所以我必须在这里发帖 将它包含在你应该做的模板中

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

或者在我的情况下它只在没有api部分的情况下工作

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

获取资源的可用URL列表,从python shell导入资源然后执行以下命令

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

你应该得到这样的东西

api_dispatch_list
api_get_schema
api_get_multiple
api_dispatch_detail

有关详细信息,或者如果您使用命名空间,请检查此项 https://github.com/toastdriven/django-tastypie/issues/409