为什么当我将@api

编程入门 行业动态 更新时间:2024-10-27 14:33:42
为什么当我将@api_view添加到我的视图中时,我得到了403(Why when I add @api_view to my view that I get a 403)

我正在关注Django 2.0中的Django REST Framework教程, http://www.django-rest-framework.org/tutorial/2-requests-and-responses/ 。 但是,当我添加@api_view装饰器时,我在视图上执行GET时会得到403。

GET /attendance/api/youths/ HTTP 403 Forbidden Allow: OPTIONS, GET Content-Type: application/json Vary: Accept { "detail": "Invalid username/password." }

这是我的代码。

@api_view(['GET']) def youths_json(request, format=None): youths = Youth.objects.filter(attending=True) serializer = YouthSerializer(youths, many=True) return Response(serializer.data)

当我添加AllowAny权限时,它仍然不起作用。

@permission_classes((AllowAny, ))

有任何想法吗? 我想得到这个工作,我真的很想使用ListAPIView 。

I was following along with the Django REST Framework tutorial on Django 2.0, http://www.django-rest-framework.org/tutorial/2-requests-and-responses/. However when I add the @api_view decorator I get a 403 when I do a GET on the view.

GET /attendance/api/youths/ HTTP 403 Forbidden Allow: OPTIONS, GET Content-Type: application/json Vary: Accept { "detail": "Invalid username/password." }

This is my code.

@api_view(['GET']) def youths_json(request, format=None): youths = Youth.objects.filter(attending=True) serializer = YouthSerializer(youths, many=True) return Response(serializer.data)

When I add the AllowAny permission it still doesn't work.

@permission_classes((AllowAny, ))

Any ideas? I would like to get this to work and I would really like to use the ListAPIView.

最满意答案

你忘了添加authentication_classes。 用必要的类添加@authentication_classes()。 要允许用户无需身份验证,请使用空元组。

You forgot to add authentication_classes. Add @authentication_classes() with necessary class. To allow user without authentication use empty tuple.

更多推荐

本文发布于:2023-07-31 22:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1348616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我将   api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!