【问题标题】:Reverse URL problem with Django and TastypieDjango 和 Tastypie 的反向 URL 问题
【发布时间】:2011-10-15 01:57:14
【问题描述】:

我们正在将我们的 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/ ^上传/ ^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.)$ ^管理员/文档/ ^TOU/$ [name='TOU'] ^$ [名称='索引'] ^管理员/ ^doc/(?P.)$ 当前网址 melotranscript/result/fbe7f421-b911-11e0-b721-001f5bf19720/ 与其中任何一个都不匹配。

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

【问题讨论】:

    标签: django django-piston tastypie


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      对于以后遇到此问题的访问者,URL的名称为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" %}
      

      【讨论】:

      • 是否可以在 django 模板{% ... %} 中使用上述代码来代替 python 代码?
      • 哇,如果没有这篇文章,这很难弄清楚。谢谢!
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2012-06-23
        • 1970-01-01
        • 1970-01-01
        • 2012-12-05
        • 2015-01-07
        • 1970-01-01
        • 2012-10-07
        • 2018-02-14
        • 1970-01-01
        相关资源
        最近更新 更多