Django All-Auth django.contrib.auth.backends.ModelBackend 无效



我对 Django 相当陌生,所以我一直在学习教程和指南。我目前正在尝试按照freeCodeCamp的youtube教程做一个电子商务Django项目。

一开始你需要安装我已经完成的Django-AllAuth。

我目前遇到的问题是,当您需要迁移已安装的应用程序时,它会在设置Authentication_Backends部分中的"django.contrib.auth.backends.ModelBackend"上向我抛出"无效语法错误"。

我的 settings.py

import os
ENVIRONMENT = os.getenv('ENVIRONMENT', 'development')
DEBUG = True
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '-05sgp9!deq=q1nltm@^^2cc+v29i(tyybv3v2t77qi66czazj'
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'core'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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'
]
ROOT_URLCONF = 'ecommerce.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_files')]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, 'db.sqlite3')
}
}
if ENVIRONMENT == 'production':
DEBUG = False
SECRET_KEY = os.getenv('SECRET_KEY')
SESSION_COOKIE_SECURE = True
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_SECONDS = 31536000
SECURE_REDIRECT_EXEMPT = []
SECURE_SSL_REDIRECT = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Auth
AUTHENTICATION_BACKENDS = (
...
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
...
)
SITE_ID = 1

我得到的错误

PS G:GitDjangoecommerce> python manage.py migrate sites
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:UsersdylanAnaconda3envsdjangoenvlibsite-packagesdjangocoremanagement__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:UsersdylanAnaconda3envsdjangoenvlibsite-packagesdjangocoremanagement__init__.py", line 325, in execute
settings.INSTALLED_APPS
File "C:UsersdylanAnaconda3envsdjangoenvlibsite-packagesdjangoconf__init__.py", line 79, in __getattr__
self._setup(name)
File "C:UsersdylanAnaconda3envsdjangoenvlibsite-packagesdjangoconf__init__.py", line 66, in _setup
self._wrapped = Settings(settings_module)
File "C:UsersdylanAnaconda3envsdjangoenvlibsite-packagesdjangoconf__init__.py", line 157, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 674, in exec_module
File "<frozen importlib._bootstrap_external>", line 781, in get_code
File "<frozen importlib._bootstrap_external>", line 741, in source_to_code
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "G:GitDjangoecommerceecommercesettings.py", line 90
'django.contrib.auth.backends.ModelBackend',
^
SyntaxError: invalid syntax

任何帮助将不胜感激。

我设法通过删除"..."来解决问题

AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
)

如果您注意到在AUTHENTICATION_BACKENDS元组/列表中有 3 个点......在开始和结束。尝试删除它们,它将正常工作。 或者简单地复制并粘贴以下代码片段:

AUTHENTICATION_BACKENDS = [
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
]

相关内容

最新更新