Django 和 Tastypie 的反向 URL 问题

发布于 2024-11-26 22:06:40 字数 1438 浏览 1 评论 0 原文

我们正在将 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'。

以及结果的调试页面:

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

^melotranscript/ ^upload/ ^melotranscript/ ^结果/(?P[^//]+)/ ^(?Presultshandler)/$ [name='api_dispatch_list'] ^melotranscript/ ^结果/(?P[^//]+)/ ^(?Presultshandler)/schema/$ [name='api_get_schema'] ^melotranscript/ ^结果/(?P[^//]+)/ ^(?Presultshandler)/set/(?P\w[\w/;-]*)/$ [name='api_get_multiple'] ^melotranscript/ ^结果/(?P[^//]+)/ ^(?Presultshandler)/(?P\w[\w/-]*)/$ [name='api_dispatch_detail'] ^melotranscript/ ^已处理/(?P.)$ ^管理员/文档/ ^TOU/$ [名称='TOU'] ^$ [名称='索引'] ^管理员/ ^doc/(?P.)$ 当前 URL melotranscript/result/fbe7f421-b911-11e0-b721-001f5bf19720/ 与其中任何一个都不匹配。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌生 2024-12-03 22:06:40

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

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

还有其他 URL 名称 那个 Tastypie 也提供

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

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

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

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" %}
我一向站在原地 2024-12-03 22:06:40

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

我的影子我的梦 2024-12-03 22:06:40

我不能写评论所以我必须在这里发帖
要将其包含在您的模板中,您应该这样做

{% 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文