I am creating a login page. There I want to receive json format at backend, with fields username and password in string format. But when I try to authenticate username and password (in views.py). I get the following error:
TypeError: Password must be a string or bytes, got BoundField.
views.py
from .serializers import event_serializer, student_serializer
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
@api_view(['POST', 'GET'])
def login_view(request):
if request.method == "POST":
serializer = student_login_serializer(data=request.data)
if serializer.is_valid():
print("serializer is valid")
username_pwd = serializer
username = username_pwd["username"]
password = username_pwd["password"]
student = authenticate(request, username=username, password=password)
if student is not None:
login(request, student)
events = Event.objects.all()
serializer = event_serializer(events, many=True)
return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
else:
return Response(serializer.data, status=status.HTTP_401_UNAUTHORIZED)
return Response(serializer.errors, status=status.HTTP_401_UNAUTHORIZED)
elif request.method == "GET":
users = Student.objects.all()
serializer = student_serializer(users, many=True)
return Response(serializer.data)
serializers.py
from rest_framework import serializers
from .models import Student, Club, member_request, Event, Comment
from django import forms
class student_login_serializer(forms.Form):
username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Username'}))
password = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Password'}))
fields = ['username', 'password']
models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class Student(AbstractUser):
pass
Please feel free to ask me for more details regarding this problem and thanks in advance.