'Attribute Error while importing views in Urls.py in django rest framework

views.py

from rest_framework.decorators import api_view
from rest_framework import status
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework import authentication, permissions
from django.contrib.auth.models import User
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from App.serializers import *
from App.models import *
from App.serializers import *


# Create your views here.
class ListUsers(APIView):
    """
    View to list all users in the system.

    * Requires token authentication.
    * Only admin users are able to access this view.
    """
    authentication_classes = [authentication.TokenAuthentication]
    permission_classes = [permissions.IsAdminUser]

    def get(self, request, format=None):
        """
        Return a list of all users.
        """
        usernames = [user.username for user in User.objects.all()]
        return Response(usernames)
 
class CustomAuthToken(ObtainAuthToken):

    def post(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data,
                                           context={'request': request})
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        token, created = Token.objects.get_or_create(user=user)
        return Response({
            'token': token.key,
            'user_id': user.pk,
            'email': user.email
        })

@api_view(['GET'])
def consol_overall_view(request):
    user = Users.objects.values('id','employee_name','billable_and_non_billable',)
    qs = conso_serializers(user, many= True)
    proj = Add_Job.objects.values('project','user','client')
    qs1 = timelog_serializers(proj, many= True)
    cli = Add_Timelog.objects.values('Date','Hours','project_id')
    qs2 = time_serializers(cli,many= True)
    return Response(qs.data+qs1.data+qs2.data,status = status.HTTP_200_OK)

urls.py

from django.contrib import admin
from django.urls import path,include
from django.urls import re_path
from django import views
from App.views import CustomAuthToken
from.router import router
from rest_framework.authtoken import views
from django.views.generic import TemplateView
from App.views import consol_overall_view

urlpatterns = [
    path('', TemplateView.as_view(template_name="social_app/index.html")), 
    path('admin/', admin.site.urls),
    path('api/',include(router.urls)),
    path('accounts/', include('allauth.urls')),
    re_path('rest-auth/', include('rest_auth.urls')),
    path('api-auth/', include('rest_framework.urls')),
    re_path('rest-auth/registration/', include('rest_auth.registration.urls')),
    path('api-token-auth/', views.obtain_auth_token),
    path('api-token-auth/', CustomAuthToken.as_view()),
    path('over',views.consol_overall_view),
]

while I tried to migrate and runserver it is showing an error

path('over',views.consol_overall_view), AttributeError: module 'rest_framework.authtoken.views' has no attribute 'consol_overall_view'

I need the consol_overall_view in Url or as a API. If it is possible to do as API kindly guide on the same.



Solution 1:[1]

When python interpreter finds imports with same name it takes the latest one into account. In your urls.py you have imported views module from two distinct packages. Python interpreter will treat the name views from the package restframework.authtoken as the views module not the one from django because the import from the first one is the latest. Apart from that, you have imported consol_overall_view directly from App.views but referring it as a value from views that you have imported before. Change views.consol_overall_view to consol_overall_view in your urlpatterns. Also, use aliases in your imports.

from django.contrib import admin
from django.urls import path,include
from django.urls import re_path
# import django.views with alias
from django import views as django_views
from App.views import CustomAuthToken
from.router import router
from rest_framework.authtoken import views
from django.views.generic import TemplateView
from App.views import consol_overall_view

urlpatterns = [
    path('', TemplateView.as_view(template_name="social_app/index.html")), 
    path('admin/', admin.site.urls),
    path('api/',include(router.urls)),
    path('accounts/', include('allauth.urls')),
    re_path('rest-auth/', include('rest_auth.urls')),
    path('api-auth/', include('rest_framework.urls')),
    re_path('rest-auth/registration/', include('rest_auth.registration.urls')),
    path('api-token-auth/', views.obtain_auth_token),
    path('api-token-auth/', CustomAuthToken.as_view()),
    path('over', consol_overall_view),
]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alamshafi2263