Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding Basic Authentication In A Single View - Django Rest Framework

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',),
}
like image 892
Nidhin Avatar asked Nov 05 '15 08:11

Nidhin


People also ask

Which authentication is best in Django Rest Framework?

Django-Knox is a framework that makes the authentication of the API endpoints built with the Django Rest Framework easier. However, Knox is also a token-based authentication like JSON Web Token (JWT) auth. Django-Knox comes with well-detailed documentation for easy implementation.

What is basic authentication in Django Rest Framework?

Basic Authentication in Django REST Framework uses HTTP Basic Authentication. It is generally appropriate for testing. The REST framework will attempt to authenticate the Basic Authentication class and set the returned values to request. user and request.


2 Answers

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
like image 81
Pandikunta Anand Reddy Avatar answered Jan 02 '23 20:01

Pandikunta Anand Reddy


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.

like image 33
Linovia Avatar answered Jan 02 '23 21:01

Linovia