sajid
sajid

Reputation: 883

Settings.py throws error when I try to import my custom backend

I have been writing a small app for which I need to authenticate users via email (as username field) So I have written a custom AuthenticationBackend. But when I import the module, I get the below error.

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty

But my secret_key field is not empty. And when I remove the import I don't get the error. Here is my auth_back.py (which contains the AuthenticationBackend)

from django.contrib.auth.models import User
from user_manager.models import AllUser
class CustomBackend(object):
    def authenticate(self, email=None, password=None):
         try:
             o = AllUser.objects.get(email=email, password=password)
         except AllUser.DoesNotExist:

                 return None
         return User.objects.get(email=o.email)
    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

Here is the import in my settings.py. I placed this at the top.

from auth_back import CustomBackend
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
AUTHENTICATION_BACKENDS = ('auth_back.MyCustomBackend', )

I placed the auth_back.py at the root folder of my project (same folder where my settings.py and wsgi.py live) Thanks in advance.

Upvotes: 0

Views: 59

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599480

There is no reason to import your backend into settings. The AUTHENTICATION_BACKENDS setting, like all the other settings, takes a string path.

Upvotes: 1

Related Questions