title: User Login Authentication using forms and Django
body: <p>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.</p>
  <p>my login.views look like this</p>
  <pre><code>def login(request):
  if request.method == &quot;POST&quot;:
      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})
  </code></pre>
  <p>My student model looks like this:</p>
  <pre><code>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 = &quot;students&quot;
  </code></pre>
  <p>My form file:</p>
  <pre><code>class StudentForm(forms.ModelForm):
      class Meta:
          model = students
          fields = &quot;__all__&quot;
  
  class Studentlogin(forms.Form):
      email = forms.EmailField(max_length=150)
      password = forms.CharField(max_length = 50, widget=forms.PasswordInput)
  </code></pre>