How to use Authentication in DRF

51 views
Skip to first unread message

Salima Begum

unread,
Jul 21, 2022, 1:08:17 AM7/21/22
to django...@googlegroups.com
Hi all,

I am trying to convert my current project(It is developed in Django) to DRF. So, I set up DRF into my project then I wrote an endpoint for after user login on the session I need to get the response when I test the my-reviews API.

models.py
```
class customer(models.Model):
    cust_id = models.IntegerField(null="true")
    email = models.CharField(max_length=100)
    # reemail = models.CharField(max_length=100, null='true')
    password = models.CharField(max_length=500)
    repassword = models.CharField(max_length=500, null='true')
    firstname = models.CharField(max_length=225)
    lastname = models.CharField(max_length=225, null=True)
    state = models.CharField(max_length=64, null=True)
    city = models.CharField(max_length=64, null=True)
    location = models.CharField(max_length=225, null=True)
    Zip = models.CharField(max_length=64)
    mailing = models.CharField(max_length=1000)
    added_date = models.DateTimeField(editable=False)
    modified_date = models.DateTimeField(null=True, blank=True)
    last_loggedin = models.DateField()
```
views.py

```
@api_view(['GET'])
def myservicereviewAPI(request):
    # If a user session is logged out it will redirect to the home page.
    if ((request.session.get('email') is None) or (request.session.get('email') == "")):
        # redirecting user after logged out to home page.
        return HttpResponseRedirect("/home")
    if request.method == 'GET':
        students = services_review.objects.all().order_by('-added_date')
        serializer = ServicesReviewSerializer(students, many=True)
        return Response(serializer.data)
```
urls.py
```
path('myservicereviewAPI', views.myservicereviewAPI, name='myservicereviewAPI'),
```
 
Results of Postman when I run 'myservicereviewAPI'

After login Browser results of 'myservicereviewAPI'

Please Help me to achieve this.

Thanks
~Salima

postmanResponse.PNG
after_login_site_Response.PNG

Ammar Mohammed

unread,
Jul 21, 2022, 1:28:25 AM7/21/22
to django...@googlegroups.com

Hey Salima
I guess you can use access token authentication in DRF.
But Do you recive the response without logging in?
Please explain more please

--

Ammar Mohammed
+249 113075979

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMSz6bk%2B73gVaO0Pfq3BfT4msHAprSwyxcCq-9BWtS-faT%3DYAA%40mail.gmail.com.

Salima Begum

unread,
Jul 21, 2022, 2:24:58 AM7/21/22
to django...@googlegroups.com
Thanks  Ammar Mohammed for the quick response, After login to the website  through Chrome browser, I got the response.
 But when I run from Postman to test API by mentioning Basic AUTH credentials of which I created user models login credentials it's giving home page html response but which I want is JSON Response. Why it's not reading credentials I mentioned there?

Thanks
~Salima


To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHs1H7vv1b5zYXTNAk-Pm9FU5r9wVCJjODi9Pmc6BCaykKRnuQ%40mail.gmail.com.

Salima Begum

unread,
Jul 21, 2022, 2:38:36 AM7/21/22
to django...@googlegroups.com
Hi all,

I am trying to convert my current project(It is developed in Django) to DRF. So, I set up DRF in my project then I wrote an endpoint for after user login on the session I need to get the response when I test the my-reviews API.

models.py

```

class customer(models.Model):
    cust_id = models.IntegerField(null="true")
    email = models.CharField(max_length=100)
    # reemail = models.CharField(max_length=100, null='true')
    password = models.CharField(max_length=500)
    repassword = models.CharField(max_length=500, null='true')
    firstname = models.CharField(max_length=225)
    lastname = models.CharField(max_length=225, null=True)
    state = models.CharField(max_length=64, null=True)
    city = models.CharField(max_length=64, null=True)
    location = models.CharField(max_length=225, null=True)
    Zip = models.CharField(max_length=64)
    mailing = models.CharField(max_length=1000)
    added_date = models.DateTimeField(editable=False)
    modified_date = models.DateTimeField(null=True, blank=True)
    last_loggedin = models.DateField()
```
views.py
```
@api_view(['GET'])
def myservicereviewAPI(request):
    # If user session is logged out it will redirect to home page.

    if ((request.session.get('email') is None) or (request.session.get('email') == "")):
        # redirecting user after logged out to home page.
        return HttpResponseRedirect("/home")
    if request.method == 'GET':
        students = vk_services_review.objects.all().order_by('-added_date')

        serializer = ServicesReviewSerializer(students, many=True)
        return Response(serializer.data)
```
urls.py
```
path('myservicereviewAPI', views.myservicereviewAPI, name='myservicereviewAPI'),
```
**Results of Postman when I run 'myservicereviewAPI'**



After login Browser results of 'myservicereviewAPI'



Please help me to achieve this.
Thanks
~Salima


postmanResponse.PNG
after_login_site_Response.PNG

Salima Begum

unread,
Jul 21, 2022, 2:58:55 AM7/21/22
to django...@googlegroups.com
Hi all,

I am trying to convert my current project(It is developed in Django) to DRF. So, I set up DRF in my project then I wrote an endpoint for after user login on the session I need to get the response when I test the my-reviews API.

models.py

```

class customer(models.Model):
    cust_id = models.IntegerField(null="true")
    email = models.CharField(max_length=100)
    # reemail = models.CharField(max_length=100, null='true')
    password = models.CharField(max_length=500)
    repassword = models.CharField(max_length=500, null='true')
    firstname = models.CharField(max_length=225)
    lastname = models.CharField(max_length=225, null=True)
    state = models.CharField(max_length=64, null=True)
    city = models.CharField(max_length=64, null=True)
    location = models.CharField(max_length=225, null=True)
    Zip = models.CharField(max_length=64)
    mailing = models.CharField(max_length=1000)
    added_date = models.DateTimeField(editable=False)
    modified_date = models.DateTimeField(null=True, blank=True)
    last_loggedin = models.DateField()

```

views.py

```

@api_view(['GET'])
def myservicereviewAPI(request
):
    # If a user session is logoff it will redirect to the home page.

    if ((request.session.get('email') is None) or (request.session.get('email') == ""
)):
        # redirecting the user after logging out to the home page.

        return HttpResponseRedirect("/home")
    if request.method == 'GET'
:
        students = services_review.objects.all().order_by('-added_date')
        serializer = ServicesReviewSerializer(students, many=True)
        return Response(serializer.data)

```

urls.py

```

path('myservicereviewAPI', views.myservicereviewAPI, name='myservicereviewAPI'),

```

Results of Postman when I run 'myservicereviewAPI'

After login Browser results of 'myservicereviewAPI'

Please Help me to achieve this.

Thanks

~Salima


postmanResponse1.PNG
after_login_site_Response.PNG

Ammar Mohammed

unread,
Jul 21, 2022, 3:09:15 AM7/21/22
to django...@googlegroups.com

How is your login view working ?
Or this is your login view ?

Please share the whole app views and urls.

(I suggest using DRF class based views as it's alot useful and helpful when it comes to authentication and permissions)

Regards,

--
Ammar Mohammed

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMSz6b%3D6HpkhJGpr5yf64GL8fta10DgfPKsbotKNVETh_d8mPA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages