Excluding Basic Authentication In A Single View -

2019-04-13 01:38发布

问题:

I set basic authentication in my setting.py as follows. Now I need a view that doesn't use basic authentication. How can I do it.

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.BasicAuthentication',),
}

回答1:

You simply need to set the authentication_classes on your view. Have a look at http://www.django-rest-framework.org/api-guide/authentication/#setting-the-authentication-scheme for an example.

Edit: To remove authentication, set the authentication_classes to an empty list. Don't forget to remove permissions as well since they usually rely on authentication.



回答2:

To exclude a view from authentication, set authentication_classes and permission_classes to [].

class SignupView(APIView):
    authentication_classes = []
    permission_classes = []

    def post(self, request):
        # view code