-
-
Save pydanny/3641787 to your computer and use it in GitHub Desktop.
login form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginForm(forms.Form): | |
username = forms.CharField(label="Username", max_length=30) | |
password = forms.CharField(label="Password", widget=forms.PasswordInput) | |
@property | |
def helper(self): | |
form = LoginForm() | |
helper = FormHelper() | |
reset = Reset('','Reset') | |
helper.add_input(reset) | |
submit = Submit('','Log In') | |
helper.add_input(submit) | |
helper.form_action = '/accounts/login' | |
helper.form_method = 'POST' | |
return helper | |
def clean(self): | |
username = self.cleaned_data.get('username') | |
password = self.cleaned_data.get('password') | |
if username and password: | |
self.user_cache = authenticate(username=username, password=password) | |
if self.user_cache is None: | |
raise forms.ValidationError("Please enter a correct username and password. Note that both fields are case-sensitive.") | |
elif not self.user_cache.is_active: | |
raise forms.ValidationError("This account is inactive.") | |
return self.cleaned_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment