Can't get details of logged in user (through otp) of a custom user model

I have created a Django application where I have created a custom user model and using login through otp. I want to get the details of the user from the custom user model after he/she is logged in on the societyadmin home page. I have used user.request.get['member_id] and request.POST.get('member_id') both on the and .html file but without any success. I get error - "Could not parse the remainder: '['member_id']' from 'request.POST.get['member']'" Following is my model-:

User = get_user_model()

class S_Society_Association_Master(AbstractBaseUser):
    member_id = models.CharField(verbose_name = "Member_ID", primary_key=True, max_length=100,  unique=True)
    member_name = models.CharField(verbose_name = "Member Name", max_length=100)
    password = models.CharField(verbose_name = "Password", default=NULL, max_length = 100, null=True, blank=True)
    member_contact_number = models.CharField(verbose_name="Phone Number", max_length=15)
    otp = models.CharField(max_length=6, blank=False, default=0)   # For HOTP Verification
    member_role = models.CharField(verbose_name="Member's Role", max_length=100, 
              choices=[("P", "President"), ("T", "Treasurer"), ("S", "Secretary"), 
              ("EC", "EC members"), ("O", "Other members")])
    member_email_id = models.EmailField(verbose_name = "Member's Email", max_length=100)
    member_from_date = models.DateField(verbose_name = "Member From", auto_now_add=True)
    member_to_date = models.DateField(verbose_name="Member To")
    created_date = models.DateField(verbose_name = "Created Date", auto_now_add = True, blank=True, null=True)
    created_by = models.ForeignKey(User, to_field='id', related_name = "assoc_created_by", on_delete = models.SET_NULL, verbose_name="Created By", max_length=100, blank=True, null=True)
    last_updated_date = models.DateField(verbose_name = "Updated Date", auto_now = True, blank=True, null=True)
    last_updated_by = models.ForeignKey(User, to_field='id', related_name = "assoc_updated_by", on_delete = models.SET_NULL, verbose_name="Last Updated By", max_length=100, blank=True, null=True)

    USERNAME_FIELD = 'member_email_id'

Following is my

def login_attempt(request):
    if request.method == 'POST':
        mobile = request.POST.get('member_id') #This is what I am using to get member details but it isn't working.
        member = S_Society_Association_Master.objects.filter(member_contact_number = mobile).first()
        if member is None:    
            context = {'message' : 'User not found' , 'class' : 'danger' }
            return render(request, 'societyadmin/sa_login.html', context)

        otp = str(random.randint(1000, 9999))
        member.otp = otp
        message_handler = MessageHandler(mobile, member.otp).send_otp_on_phone()
        request.session['mobile'] = mobile
        return redirect('login_otp')
    return render(request, 'societyadmin/sa_login.html')

def login_otp(request):
    mobile = request.session['mobile']
    context = {'mobile':mobile}
    if request.method == 'POST':
        otp = request.POST.get('otp')
        member = S_Society_Association_Master.objects.filter(member_contact_number=mobile).first() 
        if otp == member.otp:
            member_row = S_Society_Association_Master.objects.get(member_id = member.member_id)
            login(request , member_row)
            context = {'message' : 'Welcome', 'member_id': member_row.member_id, 'mobile':mobile }
            return render(request,'societyadmin/home.html' , context)
            context = {'message' : 'Wrong OTP' , 'class' : 'danger','mobile':mobile }
            return render(request,'societyadmin/sa_login_otp.html' , context)

    return render(request,'societyadmin/sa_login_otp.html' , context)

def society_admin_home(request):
    return render(request, 'societyadmin/home.html') Here are functions for sending message to mobile phone and email.

from django.conf import settings 
from import Client #I am using Twilio to send messages on phone.
from django.core.mail import send_mail
import random

class MessageHandler:

    phone_number = None
    otp = None

    def __init__(self, phone_number, otp) -> None:
        self.phone_number = phone_number
        self.otp = otp

    def send_otp_on_phone(self):
        client = Client(settings.ACCOUNT_SID, settings.AUTH_TOKEN)
        message = client.messages.create(body = f'Your otp is {self.otp}', from_ = '+19853323340', to = self.phone_number)
        return message

    def send_otp_on_email(self):
        member_name = self.member_name
        member_email = self.member_email_id
        subject = 'Login OTP'
        message = f'Hi {member_name}, Your otp to login is {self.otp}'
        email_from = settings.EMAIL_HOST_USER
        recipients = [member_email, ]
        send_mail(subject, message, email_from, recipients)
        return None

from .views import *

urlpatterns = [
    path('', login_attempt, name = "login"),
    path('home', society_admin_home , name="society_admin_home"),
    path('login_otp', login_otp , name="login_otp")   

Kindly help.




Back to Top