0
votes

Settings.py

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

models.py

from django.db import models
class Emp(models.Model):
    eid = models.IntegerField()
    ename = models.CharField(max_length=30)
    sal = models.IntegerField()
    def __str__(self):
        return self.ename

admin.py

from django.contrib import admin
from .models import Emp
class AdminEmp(admin.ModelAdmin):
    list_display = ['eid','ename','sal']
admin.site.register(Emp,AdminEmp)

serializers.py

from .models import Emp
from rest_framework import serializers
class EmpSerializer(serializers.ModelSerializer):
    class Meta:
        model = Emp
        fields = ('eid','ename','sal')

views.py

from .serializers import EmpSerializer
from .models import Emp
from rest_framework import viewsets
from rest_framework.authentication import BaseAuthentication
from rest_framework.permissions import IsAuthenticated
class EmpViewSet2(viewsets.ModelViewSet):
    authentication_classes = (BaseAuthentication,)
    permission_classes = (IsAuthenticated,)
    queryset = Emp.objects.all()
    serializer_class = EmpSerializer

app level urls.py

from django.conf.urls import url,include
from .views import EmpViewSet2
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register('emp_viewset',EmpViewSet2,base_name='emp_viewset2')
urlpatterns = [
url(r'',include(router.urls)) ]

Project level urls.py

from django.contrib import admin
from django.urls import path,include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('Basic_Authentication_App.urls'))
]

Username and password window enter image description here

Django Rest Framework window but when i click on this link "emp_viewset":"http://127.0.0.1:3499/api/emp_viewset/"

enter image description here

it shows like below: NotImplementedError at /api/emp_viewset/ .authenticate() must be overridden.

enter image description here

1

1 Answers

0
votes

You need to write your own authentication back-end. You can see an example from the official django documentation that explains in detail how to implement (i.e. override) the authenticate function.

Of course, if you want to provide your own permissions, you can implement a custom authentication back-end.