Home:ALL Converter>Login student model django

Login student model django

Ask Time:2018-07-31T22:28:37         Author:Nabeel Ayub

Json Formatter

I am learning django framework and I stuck in a Teacher/student problem.I have student model in which i have students username and password whereas teachers accounts are directly created from admin site users model.I have successfully created login for teachers by using built-in login system of django. Now I wanted to make login of students,their username/password are stored in student models.In my case,student account(username and password) are created by their teachers and are stored in student model.So student only needs to login and i have only one login form (both for teacher and student). How can I login student by checking from students models.I have searched about it but it makes me confusing.Need some help for this.

here below the login code for Teacher which are created from admin site:

       class loginform(View):             
            template_name='IEL/Login.html'
            form_class=UserForm

            def get(self,request):     # if the request is get then only view the function
                form=self.form_class(None)
                return render(request, self.template_name, {'form': form})

            def post(self,request):
                form=self.form_class(request.POST)

                if form.is_valid():
                    #user = form.save(commit=False)  # it doesnot save in database, it is used to et clean the values
                    # clean data
                    username = form.cleaned_data['username']
                    password = form.cleaned_data['password']

                    # authenticate user:
                    user = authenticate(username=username, password=password)

                    if user is not None:
                        login(request, user)
                        return redirect('IEL:file')
                else:
                    msg = 'Errors: %s' % form.errors.as_text()
                    return HttpResponse(msg, status=400)

                return render(request, self.template_name, {'form': form})  

Author:Nabeel Ayub,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51615795/login-student-model-django
yy