Skip to content

Instantly share code, notes, and snippets.

@manjurulhoque
Created March 30, 2019 14:50
Show Gist options
  • Save manjurulhoque/adf3e248e11449ec4909867135a98fd1 to your computer and use it in GitHub Desktop.
Save manjurulhoque/adf3e248e11449ec4909867135a98fd1 to your computer and use it in GitHub Desktop.
class Login(FormView):
"""
Provides the ability to login as a user with a username and password
"""
success_url = '/'
form_class = UserLoginForm
template_name = 'accounts/form.html'
extra_context = {
'title': 'Login'
}
def dispatch(self, request, *args, **kwargs):
if self.request.user.is_authenticated:
return HttpResponseRedirect(self.get_success_url())
return super().dispatch(self.request, *args, **kwargs)
def get_form_class(self):
return self.form_class
def form_valid(self, form):
auth.login(self.request, form.get_user())
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form):
"""If the form is invalid, render the invalid form."""
return self.render_to_response(self.get_context_data(form=form))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment