title: How to login either through a mobile no or a email address in django?
body: <p>I initially used to login using email and password but now want to change it in such a way that we can login using either mobile no or email address and password.I am not able to understand what parts to change in my code as I do not want to delete already existing user and data.</p>
  
  <p>forms.py</p>
  
  <pre><code>from django.contrib.auth.models import User
  from django import forms
  
  
  class UserForm(forms.ModelForm):
      password = forms.CharField(widget=forms.PasswordInput)
  
      class Meta:
          model = User
          fields = ['first_name', 'last_name', 'email', 'password']
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>class UserFormView(View):
  
      form_class = UserForm
      template_name = 'main/registration_form.html'
  
      def get(self, request):
          form = self.form_class(None)
          return render(request, self.template_name, {'form': form})
  
      def post(self, request):
          form = self.form_class(request.POST)
  
          if form.is_valid():
  
              user = form.save(commit=False)
  
              # cleaned (normalized) data
              email = form.cleaned_data['email']
              password = form.cleaned_data['password']
              user.username = email
              user.set_password(password)
              user.save()
  
              # return user objects if credentials are correct
              user = authenticate(username=email, password=password)
  
              if user is not None:
  
                  if user.is_active:
                      login(request, user)
                      # request.user.username       display their username
                      return redirect('main:register2')
          return render(request, self.template_name, {'form': form})
  
  class LoginForm(View):
  
      form_class = UserForm
      template_name = 'main/login_form.html'
  
      def get(self, request):
          form = self.form_class(None)
          if error:
              return render(request, self.template_name, {'form': form}, 
  {'error': error})
          else:
              return render(request, self.template_name, {'form': form})
  
      def post(self, request):
          email = request.POST.get('email', '')
          password = request.POST.get('password', '')
          user = auth.authenticate(username=email, password=password)
  
          if user is not None and user.is_active:
              # Correct password, and the user is marked "active"
              auth.login(request, user)
              # Redirect to a success page.
              return HttpResponseRedirect("/main/home/")
          else:
              # Show an error page
              error = True
              return HttpResponseRedirect("/main/", {'error': error})
  </code></pre>
  
  <p>Please answer considering the fact that I am new to Django. Thanks in advance :)</p>