简体   繁体   English

Django 使用相同结尾的网址

[英]Django use urls with same ending

I ahve a django project with the application web-api and the following url configuration:我有一个带有应用程序web-api和以下 url 配置的 django 项目:

main project urls.py主项目urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('web-api/', include('webapi.urls')),
]

application web-api/urls.py应用程序web-api/urls.py

urlpatterns = [
    path('<param1:param1>/summary', Param1Summary.as_view()),
    path('<param1:param1>/item/<int:id>/summary', ItemView.as_view()),
]

The /web-api/param1/summary endpoint works but /web-api/param1/item/12121/summary does not work and returns a 404 status code. /web-api/param1/summary端点有效,但/web-api/param1/item/12121/summary无效并返回 404 状态代码。

If I change the <param1:param1>/item/<int:id>/summary' to <param1:param1>/item/<int:id>/whatever , in the web-api/url.py I get the desired results.如果我将<param1:param1>/item/<int:id>/summary'更改为<param1:param1>/item/<int:id>/whatever ,在web-api/url.py我得到了所需的结果。

I have tried using url regex without succees.我尝试使用 url 正则表达式但没有成功。

Any ideas on how to solve the problem?关于如何解决问题的任何想法?

Thanks in advance!提前致谢!

Not an general answer but worked in my case.不是一般的答案,但在我的情况下有效。

Changing the order of the urls solved the issue.更改网址的顺序解决了这个问题。 The urls in web-api/urls.py had to be in the following order: web-api/urls.py中的 url 必须按以下顺序排列:

urlpatterns = [
    path('<str:param1>/item/<int:pk>/summary', ItemView.as_view()),
    path('<str:param1>/summary', Param1Summary.as_view()),
]

I'm not sure but you can reorder the urls in this way and add name spaces so you can call them from html template or from other views as well:我不确定,但您可以通过这种方式重新排序网址并添加名称空间,以便您可以从 html 模板或其他视图中调用它们:

urlpatterns = [
    path('summary/<param1:param1>', Param1Summary.as_view(), name='summary_parm')
    path('summary/<param1:param1>/item/<int:id>', ItemView.as_view(),  name='summary_item'),
]

Update your application web-api's urls.py like this.像这样更新您的应用程序 web-api 的urls.py

urlpatterns = [
    path('<param1:param1>/item/<int:id>/summary', ItemView.as_view()),
    path('<param1:param1>/summary', Param1Summary.as_view()),
]

Just swap their sequence, this will work.只需交换它们的顺序,这就可以了。

First, "param1" is not a valid url parameter type ( see the docs ), so you probably wanted to use '<str:param1>/summary' and '<str:param1>/item/<int:pk>/summary' .首先,“param1”不是有效的 url 参数类型( 请参阅文档),因此您可能想使用'<str:param1>/summary''<str:param1>/item/<int:pk>/summary' .

Second, you should name the url variable "pk" as mentioned in django docs here , and not "id".其次,您应该将 url 变量命名为“pk”,如django 文档中所述,而不是“id”。

it looks for pk and slug keyword arguments as declared in the URLConf, and looks the object up either from the model attribute on the view, or the queryset attribute if that's provided它查找 URLConf 中声明的 pk 和 slug 关键字 arguments,并从视图上提供的 model 属性或查询集属性向上查找 object

It should look like this:它应该如下所示:

urlpatterns = [
    path('<str:param1>/summary', Param1Summary.as_view()),
    path('<str:param1>/item/<int:pk>/summary', ItemView.as_view()),
]

Alternatively, you can set the pk_url_kwarg = "id" on your view to let the get_object() method know how to query for the object.或者,您可以在视图上设置pk_url_kwarg = "id"以让get_object()方法知道如何查询 object。 See in the docs 在文档中查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM