Add LoginForm

This commit is contained in:
PCoder 2019-02-19 23:16:05 +01:00
commit a6a6f8213b
2 changed files with 45 additions and 16 deletions

40
dal/forms.py Normal file
View file

@ -0,0 +1,40 @@
from django import forms
from django.contrib.auth import authenticate
from django.shortcuts import render
from django.utils.translation import ugettext_lazy as _
class LoginForm(forms.Form):
email = forms.CharField(widget=forms.TextInput())
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
fields = ['email', 'password']
def clean(self):
email = self.cleaned_data.get('email')
password = self.cleaned_data.get('password')
if self.errors:
return self.cleaned_data
is_auth = authenticate(username=email, password=password)
if not is_auth:
raise forms.ValidationError(
_("Your username and/or password were incorrect."))
elif is_auth.validated == 0:
raise forms.ValidationError(
_("Your account is not activated yet."))
return self.cleaned_data
def clean_email(self):
email = self.cleaned_data.get('email')
return email
# try:
# CustomUser.objects.get(email=email)
# return email
# except CustomUser.DoesNotExist:
# raise forms.ValidationError(_("User does not exist"))
def form_valid(self, form):
form.save()
return render(self.request, 'useroptions.html', {'user': self.get_context_data()})