Search code examples
djangodjango-class-based-viewsdjango-request

AttributeError: 'bytes' object has no attribute


I have a class in which I am checking user permissions, and depending on them, I return a list of results from models. Here is what I have:

from AnnualLeave.models import Leaves, RejectedLeaves, Employee

class GetLeaves:

    def get_results(request):
        try:
            if request.user.groups.filter(name__in=['SuperAdmin']).exists():
                return Leaves.objects.all()
            elif request.user.groups.filter(name__in=['Admin']).exists():
                return Leaves.objects.filter(employee_id=Employee.objects.get(manager_id=request.user.pk))
            else:
                messages.error(request, "You are not allowed on this page")
                return render(request, 'users/home.html')

        except (Leaves.DoesNotExist, Employee.DoesNotExist):
            return []

    def process_results(request):
        leave_request = []
        for leave in GetLeaves.get_results(request):
            content = {'emp_id': leave.employee_id,
                       'emp_name': Employee.objects.get(user_id=leave.employee_id).user.get_full_name(),
                       'start_date': leave.start_date,
                       'end_date': leave.end_date,
                       'reason': leave.get_reason_display(),
                       'description': leave.description,
                       }
        ....

            leave_request.append(content)

        return leave_request

And then I'm calling process_results function in TemplateView like this:

class ProcessLeaveRequest(TemplateView):
    template_name = 'LMSAdmin/process_leave_request.html'

    def get(self, request, *args, **kwargs):
        return render(request, self.template_name, {'leave_requests': GetLeaves.process_results(request)})

Here is a traceback of error:

Internal Server Error: /lms_admin/upcomingleaves/
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "D:\Projects\LMS\LMSAdmin\views.py", line 77, in get
    for leave in GetLeaves.process_results(request):
  File "D:\Projects\LMS\LMSAdmin\views.py", line 39, in process_results
    content = {'emp_id': leave.employee_id,
AttributeError: 'bytes' object has no attribute 'employee_id'

From what I can see, it is reading the entire webpage with the request parameter in functions. Like the get_results function is not returning a list, but the entire webpage. I do not understand why this is happening because it was working fine before without any changes.


Solution

  • You should not return a HttpRequest from get_results. Instead raise a PermissionDenied error.

    else:
        from django.core.exceptions import PermissionDenied
        messages.error(request, "You are not allowed on this page")
        raise PermissionDenied
    

    If you want to redirect to a different page, handle that in the view. You can capture the PermissionDenied and return the redirect.