Wednesday, July 20, 2022

How to use Authentication in DRF

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

--
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.

No comments:

Post a Comment