Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

User Login Authentication using forms and Django

I am trying to set up the user authentication for the login page using forms and comparing it to database values and my code works but then I realized I was getting login successful if I put any password if it was available in the database. What I want to do is to search for the mail and get the password for that user only not the whole database. My database will contain no duplicate emails so I don’t have to worry about that. I have spend too much time trying to figure out how to get the password for same user the email is.

my login.views look like this

def login(request):
if request.method == "POST":
    form = Studentlogin(request.POST)
    if form.is_valid():
        email = form.cleaned_data.get('email')
        password = form.cleaned_data.get('password')
        user = User.objects.create_user(email, password)
        try:
            studentemail = students.objects.get(email=email)
            studentpass = students.objects.get(password=password)
            return render (request, 'subscrap/main.html', {'student': studentemail })
        except:
            messages.success(request, 'Error, either Email or Password is not correct')
            pass
else:
    form = Studentlogin()
return render(request, 'subscrap/login.html', {'form': form})

My student model looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class students(models.Model):
fname = models.CharField(max_length=50)
lname = models.CharField(max_length=50)
password = models.CharField(max_length = 50 , null = True)
passwordrepeat = models.CharField(max_length = 50, null = True)
email = models.EmailField(max_length=150)
class Meta:
    db_table = "students"

My form file:

class StudentForm(forms.ModelForm):
    class Meta:
        model = students
        fields = "__all__"

class Studentlogin(forms.Form):
    email = forms.EmailField(max_length=150)
    password = forms.CharField(max_length = 50, widget=forms.PasswordInput)

>Solution :

You need to make only one query to get the student with the given email and password:

def login(request):
    if request.method == "POST":
        form = Studentlogin(request.POST)
        if form.is_valid():
            email = form.cleaned_data.get('email')
            password = form.cleaned_data.get('password')
            user = User.objects.create_user(email, password)
            try:
               studentemail = students.objects.get(email=email, password=password)
               return render (request, 'subscrap/main.html', {'student': studentemail })
            except:
               messages.success(request, 'Error, either Email or Password is not correct')
        else:
           form = Studentlogin()
        return render(request, 'subscrap/login.html', {'form': form})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading