admin管理员组

文章数量:1290959

I'm facing a strange issue with Django REST Framework (DRF).

# views.py
class CheckoutView(APIView):    
    permission_classes = [AllowAny]
    
    def post(self, request, *args, **kwargs):
        return Response({'total_price': 7879})

#url.py
urlpatterns = [
    path("cart/checkout/<int:new>", checkoutView.as_view() , name="checkout"), # url 1
    path("cart/checkout/", checkoutView.as_view() , name="checkout"), # url 2
]

issue :
if i hit with url 1 it gives response 200

if i hit url 2 it gives response 401 { "detail": "Authentication credentials were not provided." }

note that : 'permission_classes = [AllowAny]' is there in the view also i dont have defined default permission class in settings.py

I'm facing a strange issue with Django REST Framework (DRF).

# views.py
class CheckoutView(APIView):    
    permission_classes = [AllowAny]
    
    def post(self, request, *args, **kwargs):
        return Response({'total_price': 7879})

#url.py
urlpatterns = [
    path("cart/checkout/<int:new>", checkoutView.as_view() , name="checkout"), # url 1
    path("cart/checkout/", checkoutView.as_view() , name="checkout"), # url 2
]

issue :
if i hit with url 1 it gives response 200

if i hit url 2 it gives response 401 { "detail": "Authentication credentials were not provided." }

note that : 'permission_classes = [AllowAny]' is there in the view also i dont have defined default permission class in settings.py

Share Improve this question asked Feb 13 at 15:34 Irfan KIrfan K 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

There is a difference between authenticating and permissions. An APIView has an .authentication_classes [drf-doc] attribute as well, this determines how to check if a user has logged in. By default this has BasicAuthentication and SessionAuthentication.

Even if you thus don't need to have any permission, it will just run the authentication logic, and if there is for example a HTTP_AUTHORIZATION header in the request, it needs to be formatted for example like basic username:password.

You thus should look what authentication header you send to the view, and very likely it does not follow the right structure.

本文标签: Django DRF showing weired auth methode respective to URLStack Overflow