• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

233
Views
Django current user is AnonymousUser

I have a pretty simple problem. I have a class that is extending APIView in order to pass in a Json Response to a URL which will be used as an API. I'm trying to filter data from a model so that I only get data items which belong to the current user. Because of this, I'm trying to access request.user.id to set my filter with.

From my views.py, here is a snippet of the code I am using:

class ChartData(APIView):
    authentication_classes = []
    permission_classes = []


    def get(self, request, format=None):

        print(self.request.user)

When I go to the url associated with this class, the print statement returns AnonymousUser, as if the user does not exist.

In the same views.py file I have another method (not inside any class) set up as the following:

@login_required
def LogDisplay(request):

    print(request.user.id)

However, when I go to the url associated with this method, I get an integer as an output on my console.

How can I make it so that my first method has access to the current user as well?

about 3 years ago · Santiago Trujillo
1 answers
Answer question

0

You are close.

in settings.py:

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

in views.py:

from rest_framework.permissions import IsAuthenticated

class ChartData(APIView):
    authentication_classes = []
    permission_classes = [IsAuthenticated, ]

get(self, request, format=None):
    ...
about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error