Problem when defining a backend.py django

0

I am extending the functionality of Django users and have created my own backend as follows:

backends.py

from home.models import Usuario

class UserAuthentificacionBackend(object):
    def authenticate(self, username=None, password=None):
        try:
            user = Usuario.objects.get(email=username)
            # en este punto, debes verificar la contraseña, yo lo hare como lo hace el modelo de usuario de django, siguiendo los metodos que trae este
            if user.check_password(password):
                return user
        except Usuario.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return Usuario.objects.get(id=user_id)
        except:
            return None

The class of my model that uses the backend. views.py

elif 'logInButton' in request.POST:
                if login_email and login_password:
                    try:
                        user = authenticate(username=request.POST['login_email'], password=request.POST['login_password'])
                        if user is not None and (user.activated):

my setting.py file:

AUTHENTICATION_BACKENDS = ('home.UserAuthentificacionBackend',)

home is an app that I created.

The error that throws me:

  

local variable 'user' referenced before assignment

     

During handling of the above exception (name 'authenticate' is not   defined), another exception occurred:

However, I can successfully create users using the backend.

What am I doing wrong?

    
asked by XBoss 01.07.2018 в 11:50
source

1 answer

0

It seems that the problem came from the except.

I had:

elif 'logInButton' in request.POST:
                if login_email and login_password:
                    try:
                        user = authenticate(username=request.POST['login_email'], password=request.POST['login_password'])
                        if user is not None and (user.activated):
                    except user.DoesNotExist:

and of course, user is not defined.

    
answered by 01.07.2018 / 12:27
source