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?