Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4071054
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:42:27+00:00 2026-05-20T16:42:27+00:00

Django newbie here. I wrote simplified login form which takes email and password. It

  • 0

Django newbie here.

I wrote simplified login form which takes email and password. It works great if both email and password are supplied, but if either is missing i get KeyError exception. According to django documentation this should never happen:

By default, each Field class assumes the value is required, so if you pass an empty value — either None or the empty string (“”) — then clean() will raise a ValidationError exception

I tried to write my own validators for fields (clean_email and clean_password), but it doesn’t work (ie I get KeyError exception). What am I doing wrong?

class LoginForm(forms.Form):
    email = forms.EmailField(label=_(u'Your email'))
    password = forms.CharField(widget=forms.PasswordInput, label=_(u'Password'))

    def clean_email(self):
        data = self.cleaned_data['email']
        if not data:
            raise forms.ValidationError(_("Please enter email"))
        return data

    def clean_password(self):
        data = self.cleaned_data['password']
        if not data:
            raise forms.ValidationError(_("Please enter your password"))
        return data

    def clean(self):
        try:
            username = User.objects.get(email__iexact=self.cleaned_data['email']).username
        except User.DoesNotExist:
            raise forms.ValidationError(_("No such email registered"))
        password = self.cleaned_data['password']

        self.user = auth.authenticate(username=username, password=password)
        if self.user is None or not self.user.is_active:
            raise forms.ValidationError(_("Email or password is incorrect"))
        return self.cleaned_data
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:42:28+00:00Added an answer on May 20, 2026 at 4:42 pm

    You could leverage Django’s built-in way to override how Authentication happens by setting
    AUTHENTICATION_BACKENDS in your settings.py

    Here’s my EmailAuthBackend:

    #settings.py
    AUTHENTICATION_BACKENDS = (
        'auth_backend.auth_email_backend.EmailBackend',
        'django.contrib.auth.backends.ModelBackend',
    )
    
    #auth_email_backend.py
    from django.contrib.auth.backends import ModelBackend
    from django.forms.fields import email_re
    from django.contrib.auth.models import User
    
    class EmailBackend(ModelBackend):
        """
        Authenticate against django.contrib.auth.models.User
        """
    
        def authenticate(self, **credentials):
            return 'username' in credentials and \ 
                self.authenticate_by_username_or_email(**credentials)
    
        def authenticate_by_username_or_email(self, username=None, password=None):
            try:
                user = User.objects.get(email=username)
            except User.DoesNotExist:
                try:
                    user = User.objects.get(username=username)
                except User.DoesNotExist:
                    user = None
            if user:
                return user if user.check_password(password) else None
            else:
                return None
    
        def get_user(self, user_id):
            try:
                return User.objects.get(pk=user_id)
            except User.DoesNotExist:
                return None
    
    #forms.py
    #replaces the normal username CharField with an EmailField
    from django import forms
    from django.contrib.auth.forms import AuthenticationForm
    
    class LoginForm(AuthenticationForm):
        username = forms.EmailField(max_length=75, label='Email')
        next = forms.CharField(widget=forms.HiddenInput)
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Django newbie here, I need to do a count over a certain filter in
Django newbie here, I have several types of models, in each of them the
Django newbie here, I'm using render_to_response('example.html', { 'error_message': error_message, }, context_instance=RequestContext(request)) How do I
PostgreSQL / Django / newbie here. I've got a bunch of JSON data with
Django and south newbie here I need to change the encoding of a table
Django newbie question.... I'm trying to write a search form and maintain the state
Django newbie here stumbling my way around the docs. I'm trying to create a
Python and django newbie question, here is code: class Client(User) #some fields client=Client() client.save()
Newbie question here. I am trying to use the django-extjs library in my django
I'm a django newbie. Here is my problem.... My main urls.py has a rule

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.