简体   繁体   English

List_rount在Django-rest-framework中不起作用

[英]List_rount doesn't work in Django-rest-framework

According to this link http://www.django-rest-framework.org/api-guide/routers/ 根据此链接http://www.django-rest-framework.org/api-guide/routers/

class UserViewSet(ModelViewSet):
    ...

    @detail_route(methods=['post'], permission_classes=[IsAdminOrIsSelf])
    def set_password(self, request, pk=None):

The following URL pattern would additionally be generated: 另外会生成以下URL模式:

  • URL pattern: ^users/{pk}/set_password/$ Name: 'user-set-password' 网址格式: ^users/{pk}/set_password/$名称:'user-set-password'

And here is my code: 这是我的代码: 在此处输入图片说明

I want to generate ^calendars/{pk}/events/$ URL pattern. 我想生成^calendars/{pk}/events/$ URL模式。 But it generated ^calendars/events/{pk}/$ according to below error page 但是它根据下面的错误页面生成了^calendars/events/{pk}/$ 在此处输入图片说明

So where is the problem? 那么问题出在哪里呢?

You used the wrong decorator in your code. 您在代码中使用了错误的装饰器。 You want @detail_route , not @list_route . 您需要@detail_route ,而不是@list_route @list_route does not accept a pk and therefore generates /calendars/events/ @list_route不接受pk,因此会生成/calendars/events/

Your method should be decorated like this: 您的方法应该像这样修饰:

@detail_route(methods=['get'])
def events(self, request, pk=None):
    ...

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

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