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

标签 python django django-allauth

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

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

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

我的设置.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:\Git\Django\ecommerce> 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:\Users\dylan\Anaconda3\envs\djangoenv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\dylan\Anaconda3\envs\djangoenv\lib\site-packages\django\core\management\__init__.py", line 325, in execute
    settings.INSTALLED_APPS
  File "C:\Users\dylan\Anaconda3\envs\djangoenv\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__

    self._setup(name)
  File "C:\Users\dylan\Anaconda3\envs\djangoenv\lib\site-packages\django\conf\__init__.py", line 66, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\dylan\Anaconda3\envs\djangoenv\lib\site-packages\django\conf\__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:\Git\Django\ecommerce\ecommerce\settings.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',
)

关于python - Django All-Auth django.contrib.auth.backends.ModelBackend 语法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60263510/

相关文章:

python - 安装了 Django AllAuth,找不到登录或注册

python - 当用户使用 django all-auth 注册时如何创建新的模型对象?

python - Django STATIC_FILE 没有像我想的那样工作?

django - 获取 django.db.utils.IntegrityError : duplicate key value violates unique constraint But value does not exists

python - 用户可以将帐户切换到另一个用户

当检索到的电子邮件与现有用户的电子邮件匹配时,django allauth facebook 重定向到注册?

python - 由于 gcc-4.2 无法安装 pythonpillow

python - 访问 token 刷新错误: invalid_grant from oauth2client

python - 如何随机化现有的字节数组?

python - 进程未到达 try block 内的一行