2014-04-15 24 views
7

Cách thích hợp để nhập phần phụ trợ tùy chỉnh trong settings.py là gì? Tôi hiện có sau trong settings.py:Django AUTHENTICATION_BACKENDS lỗi nhập

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

nơi apployment_site là ứng dụng, auth là tên tập tin, và CustomAuth là tên lớp.

Theo quan điểm của tôi, tôi nhận được: ImportError: a doesn't look like a module path sau khi tôi chạy đoạn mã sau:

from django.contrib.auth import authenticate 
from apployment_site import * 
authenticate(username="username", password="password") 

Dưới đây là settings.py đầy đủ của tôi:

"""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/' 

Trả lời

15

chắc chắn rằng đó là một tuple:

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

lưu ý các dấu phẩy ở cuối

+0

Điều này đúng, cảm ơn! Tôi cũng đã phải xóa "ứng dụng". Dòng chính xác là AUTHENTICATION_BACKENDS = ('apployment_site.auth.CustomAuth',) –

+0

tôi có cùng vấn đề này nhưng AUTHENTICATION_BACKENDS = ('apployment_site.auth.CustomAuth',) không hoạt động đối với tôi. nó nói ImportError: Không có module nào có tên apployment_site.auth –

+0

@Nitheesh, tên ứng dụng tùy chỉnh của bạn có thực sự là 'apployment_site', hay nó có gì khác? Và mô-đun python của bạn 'custom_app_name.auth.py' chứa lớp' CutomAuth'? –

0

Tôi không nghĩ rằng bạn cần phải thực sự nhập khẩu nó vào số view của bạn. Theo số the documentation, Django sẽ đi qua tất cả các chương trình phụ trợ xác thực của bạn khi bạn gọi authenticate().

Do đó, chỉ cần đảm bảo bạn có tất cả các chương trình phụ trợ xác thực bạn muốn trong số settings.py.

+0

Đúng vậy, nhưng Django dường như không thích cách tôi đang tham khảo phụ trợ tùy chỉnh của tôi trong settings.py. Bạn có biết cú pháp đúng là gì không? Cả 'apps.apployment_site.auth.CustomAuth' lẫn apployment_site.auth.CustomAuth'work –

+0

Cấu trúc thư mục của bạn là gì? CustomAuth của bạn nằm ở đâu? Ngoài ra, tại sao bạn có 'từ apployment_site import *' trong mã số lượt xem của bạn? Đó không phải là những gì ném lỗi? – Alex

+0

Không, đó chỉ là nhập khẩu các mô hình của tôi. Lỗi nằm trong settings.py của tôi khi tôi xác định authentication_backend. Hiện tại, auth là một tệp được xác định trong ứng dụng apployment_site của tôi và lớp chính được gọi là CustomAuth. Đó là lý do tại sao tôi giả định AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth') là cách phù hợp để tham khảo điều này. –

Các vấn đề liên quan