Search code examples
djangoapidjango-rest-frameworkdjango-rest-viewsets

router.register(), AttributeError: module 'rest_framework.views' has no attribute


I don't know what I am doing wrong. I have been battling with this error for hours. I have opened all the suggestions I saw and implemented what they suggested but still, the error is pending

router.register(r'^hmos/$', views.HMOList), AttributeError: module 'rest_framework.views' has no attribute 'HMOList'

This is "core/urls.py"

from django.conf.urls import url
from  .views import *
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from rest_framework_jwt.views import obtain_jwt_token,refresh_jwt_token
from rest_framework.routers import DefaultRouter
router = DefaultRouter()

 router.register('hmos', views.HMOList)

urlpatterns = format_suffix_patterns(urlpatterns)

This is "core/views.py"

from django.shortcuts import render_to_response
import json
from rest_framework.parsers import MultiPartParser, FileUploadParser, FormParser
from django.db.models import Q
from rest_framework import permissions
from django.contrib.auth import authenticate, login,logout
from rest_framework import generics, status, views
from rest_framework.permissions import IsAuthenticated
from .models import *
from  .serializers import *
from rest_framework.response import Response
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.permissions import IsAuthenticated
from .utils import generate_responder_serial
from rest_framework.parsers import MultiPartParser, FileUploadParser, FormParser
from django.conf import settings
import os
from django.db.models import Q
#from rest_framework.authentication import (BaseJSONWebTokenAuthentication)
from rest_framework import viewsets 

def jwt_response_payload_handler(token, user=None, request=None):
 return {
 'token': token,
 'user': SystemUserSerializer(user).data
    } 

def create(self, request, *args, **kwargs):
        new_data = {'name': request.data['name'].strip(), 'address': request.data['address'], 
 'state': request.data['state'], 'mobile1': request.data['mobile1'],
 'mobile2': request.data['mobile2'], }
 if HMO.objects.filter(name = request.data['name'].strip()):
 raise serializers.ValidationError('HMO name already exists')
        serializer = HMOSerializer(data=new_data)
 if serializer.is_valid():
 try:
                serializer.save()
 except Exception as e:
 return Response( e)

 return Response(serializer.data, status=status.HTTP_201_CREATED)
 return Response({
 'status' : 'Bad request',
 'message': 'HMO could not be created with received data.',
 'errors' : serializer.errors # for example
            }, status=status.HTTP_400_BAD_REQUEST)

This is promedic/urls.py

from django.conf.urls import url, include
from django.urls import path
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.views import serve
from rest_framework_swagger.views import get_swagger_view

schema_view = get_swagger_view(title='Pastebin API')

urlpatterns = [
    url(r'^$', schema_view),
    url(r'^admin/', admin.site.urls),
    url('api/core/', include('core.urls')),
]

Solution

  • In "core/urls.py" you should have :

    from  .views import HMOList
    router.register(r'hmos', HMOList)
    urlpatterns = router.urls