7

我们正在将我们的 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")

这使用了活塞,所以我们想把它改成美味派的东西

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”的反向。

结果的Debugpage:

使用 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/ ^processed/(?P. )$ ^admin/doc/ ^TOU/$ [name='TOU'] ^$ [name='index'] ^admin/ ^doc/(?P. )$ 当前的 URL,melotranscript/result/fbe7f421-b911-11e0-b721-001f5bf19720/,与其中任何一个都不匹配。

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

4

3 回答 3

35

对于以后遇到此问题的访问者,URL 的名称是api_dispatch_list,您还需要指定 API 名称:

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

Tastypie 还提供了其他 URL 名称:

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

您可以在调用中使用它们来反转,您可以在 HTML 中使用它们,如下所示:

{% url "api_get_schema" resource_name="myresource" api_name="v1" %}
于 2011-11-26T10:59:25.080 回答
3

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

于 2011-09-24T09:57:10.030 回答
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

于 2014-02-22T16:31:49.903 回答