How to create a login approval

Hi everyone,
I have created a website with models as alumini,college . In that website I have made sure that each alumini is associated to a college.Now what i want to create is whenever a new alumini fills the information in registration page and click register under a particular college ,then that college should approve that alumini in order to register till then the alumini should see a waiting page but shouldn’t be logged into the website .
so how to create such a approval .
Is there any tutorials regarding this ,if so please share it here or else I can share the code of my website about the models so that you can guide me to achieve the result

1 Like

The default User model has an is_active attribute.
You could:

  • initialize this to False when the User registers
  • provide a view for the person responsible for a college to see everyone registered for that college with is_active=False, allowing them to change that flag to True
  • In the login process, redirect someone with is_active=False to the “waiting page”.
1 Like

here are my views.py regarding login and register

def login_view(request):
    if request.method == 'POST':
        form = AuthenticationForm(request=request, data=request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                messages.info(request, f"You are now logged in as {username}")
                return HttpResponseRedirect(reverse('Users:index'))
            else:
                messages.error(request, "Invalid username or password.")
        else:
            messages.error(request, "Invalid username or password.")
    form = AuthenticationForm()
    return render(request = request,
                    template_name = "Users/login.html",
                    context={"form":form})
def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse('Users:index'))

def alumini_register(request):
    if request.method !='POST':
        form=AluminiSignUpForm()
    else:
        form=AluminiSignUpForm(data=request.POST)

        if form.is_valid():
            new_user=form.save()
            # log the user in and then redirect to home page.
            authenticated_user=authenticate(username=new_user.username,password=request.POST['password1'])
            login(request,authenticated_user)
            return HttpResponseRedirect(reverse('Users:index'))
    context={'form':form}
    return render(request,'Users/alumini_register.html',context)

def college_register(request):
    if request.method !='POST':
        form=CollegeSignUpForm()
    else:
        form=CollegeSignUpForm(data=request.POST)

        if form.is_valid():
            new_user=form.save()
            # log the user in and then redirect to home page.
            authenticated_user=authenticate(username=new_user.username,password=request.POST['password1'])
            login(request,authenticated_user)
            return HttpResponseRedirect(reverse('Users:index'))
    context={'form':form}
    return render(request,'Users/college_register.html',context)

Here is the models.py regarding the college and alumini

class College(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
    def __str__(self):
        return self.user.username
class Alumini(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
    college=models.ForeignKey('College',on_delete=models.CASCADE,default=1)
    def __str__(self):
        return self.user.username

so how to achieve the result, please guide me or else share any tutorial articles related to do this
Thank You

Okay I haven’t see this that’s why I shared the code . Okay I’ll try

Sir ,I have done everything but made a small mistake at defining the view , here is the view

def approve(request,alumini_user_id):
    alumini=Alumini.objects.get(user_id=alumini_user_id)
    alumini.user.is_active=True
    return HttpResponseRedirect(reverse('Users:alumini_list'))

Here is the Alumini model


[quote="MAHANTH-wq, post:3, topic:5005"]

class Alumini(models.Model):
user=models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
college=models.ForeignKey(‘College’,on_delete=models.CASCADE,default=1)
def str(self):
return self.user.username

And here is the Html file

<ul>
  {% for alumini_unapproved in aluminis_unapproved %}
  <li>
     <p> {{ alumini_unapproved }} {{ alumini_unapproved.user.is_active }} </p>
     <p><a href="{% url 'Users:approve' alumini_unapproved.user.id %}">approve</a></p>
  </li>
  {% empty %}
<li>No aluminis are to be approved yet.</li>
  {% endfor %}
  </ul>

I think I;m making a mistake in view while getting the alumini object ,just have a look what should I use there

After you make the change to the Alumini object, you need to save it back to the database.

Thank You ,Issue solved