Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django身份验证\u后端导入错误_Python_Django_Django Authentication_Django Settings - Fatal编程技术网

Python Django身份验证\u后端导入错误

Python Django身份验证\u后端导入错误,python,django,django-authentication,django-settings,Python,Django,Django Authentication,Django Settings,在settings.py中导入自定义后端的正确方法是什么?我当前在settings.py中有以下内容: AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth') """Django settings for apployment project. For more information on this file, see https://docs.djangoproject.com/en/dev/topics/se

在settings.py中导入自定义后端的正确方法是什么?我当前在settings.py中有以下内容:

AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth')
"""Django settings for apployment project.

For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '3(a=jr=tfedkqzv3f=495%0$ygxjt332(=n0&h=e2bzh(i#r*j'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'apployment_site'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth')

ROOT_URLCONF = 'apployment.urls'

WSGI_APPLICATION = 'apployment.wsgi.application'


# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/

STATIC_URL = '/static/'
其中,apployment_site是应用程序,auth是文件名,CustomAuth是类名

在我看来,在运行以下代码后,我得到:
ImportError:a看起来不像模块路径

from django.contrib.auth import authenticate
from apployment_site import *
authenticate(username="username", password="password")
以下是我的完整设置.py:

AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth')
"""Django settings for apployment project.

For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '3(a=jr=tfedkqzv3f=495%0$ygxjt332(=n0&h=e2bzh(i#r*j'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'apployment_site'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth')

ROOT_URLCONF = 'apployment.urls'

WSGI_APPLICATION = 'apployment.wsgi.application'


# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/

STATIC_URL = '/static/'

我认为您实际上不需要将其导入到
视图中。根据,当您调用
authenticate()
时,Django将检查您的所有身份验证后端


因此,只需确保在
settings.py
中具有所需的所有身份验证后端

确保它是一个元组:

AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth',)

注意右边末尾的逗号,但是Django似乎不喜欢我在settings.py中引用自定义后端的方式。你知道它的正确语法是什么吗?“apps.apployment\u site.auth.CustomAuth”和“apployment\u site.auth.CustomAuth”都不工作您的目录结构是什么?您的CustomAuth位于哪里?另外,为什么您的视图代码中有来自apployment\u站点导入的
*
?这不是抛出错误的原因吗?不,这只是导入我的模型。当我定义身份验证时,错误在my settings.py中。目前,auth是在my apployment_站点应用程序中定义的文件,主类称为CustomAuth。这就是为什么我假设AUTHENTICATION\u BACKENDS=('apps.apployment\u site.auth.CustomAuth')是引用此内容的正确方法。您可以发布整个
settings.py
文件吗?奇怪的是,上面说的“a”看起来不像模块路径。当然,我编辑了我的原始帖子来包含它。谢谢你的帮助!这是正确的,谢谢!我还必须删除“应用程序”。正确的行是AUTHENTICATION\u BACKENDS=('apployment\u site.auth.CustomAuth',)我有同样的问题,但AUTHENTICATION\u BACKENDS=('apployment\u site.auth.CustomAuth',)对我不起作用。上面写着ImportError:没有名为apployment_site的模块。auth@Nitheesh,您的自定义应用程序的名称实际上是
apployment\u站点
,还是有所不同?您的python模块
custom\u app\u name.auth.py
是否包含
CutomAuth
类?