[英]'List' object has no attribute 'startswith' - difficulty solving
在查看有關此主題的現有案例后,我還沒有找到解決方案。
我正在嘗試運行我的 python 服務器並繼續運行此回溯:
G:\inetpub\FaulknerandSonsLTD2\Website\Website>manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\threading.py", line 926, in
_bootstrap_inner
self.run()
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\site-packages\django\utils\autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\site-packages\django\core\management\__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\importlib\__init__.py", line 118, in import_module
if name.startswith('.'):
AttributeError: 'list' object has no attribute 'startswith'
我嘗試在整個項目中搜索短語“startswith”。
關於在哪里找到問題的根源有什么明顯的建議嗎? 通常,回溯會提供有關文件位置的重要線索。
應用程序設置:
"""
Django settings for Website project.
Based on 'django-admin startproject' using Django 2.1.2.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""
import os
import posixpath
import pyodbc
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '230f1c1d-ccaf-4db6-8d0c-afda7968d275'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
AUTH_USER_MODEL = 'Accounts.Account'
LOGOUT_REDIRECT_URL = 'home'
LOGIN_REDIRECT_URL = 'home'
LOGIN_URL = '/Website/login'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # existing backend
'allauth.account.auth_backends.AuthenticationBackend',
)
SITE_ID = 1
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
# Application references
# https://docs.djangoproject.com/en/2.1/ref/settings/#std:setting-INSTALLED_APPS
INSTALLED_APPS = (
"app",
"Website",
"Users.django_registration",
"Users.current_user",
"computedfields",
# Accounting Applications Here
"Accounting",
# Education Applications Here
"Education",
"Education.Examinations",
"Education.Grades",
"Education.Homework",
"Education.Lessons",
"Education.Modules",
"Education.Qualifications",
"Education.Subjects",
"Education.Subjects.ComputerScience",
"Education.Subjects.English",
"Education.Subjects.Geography",
"Education.Subjects.History",
"Education.Subjects.Mathematics",
"Education.Subjects.Music",
"Education.Subjects.PSHE",
"Education.Subjects.ReligiousStudies",
"Education.Subjects.Science",
# Healthcare Applications Here
"Healthcare",
"Healthcare.Asthma",
"Healthcare.AtrialFibrillation",
"Healthcare.BloodPressure",
"Healthcare.Cancer",
"Healthcare.CervicalScreening",
"Healthcare.ChronicHeartDisease",
"Healthcare.ChronicKidneyDisease",
"Healthcare.COPD",
"Healthcare.CVD",
"Healthcare.Dementia",
"Healthcare.Depression",
"Healthcare.Diabetes",
"Healthcare.Epilepsy",
"Healthcare.HeartFailure",
"Healthcare.Hypertension",
"Healthcare.LearningDisability",
"Healthcare.MentalHealth",
"Healthcare.Obesity",
"Healthcare.Osteoporosis",
"Healthcare.PAD",
"Healthcare.PalliativeCare",
"Healthcare.RheumatoidArthritis",
"Healthcare.Smoking",
"Healthcare.Stroke",
# Legal Applications
"Legals",
"Legals.Cookies",
"Legals.Privacy",
# Media Applications
"Media",
"Media.Books",
"Media.Radio",
"Media.RSS",
"Media.TV",
# Social Services Applications
"SocialServices",
# User Applications
"Users",
"Users.Accountants",
"Users.Accounts",
"Users.Dentists",
"Users.Doctors",
"Users.Headteachers",
"Users.Lawyers",
"Users.Parents",
"Users.Politicians",
"Users.Staff",
"Users.Students",
"Users.Teachers",
# Add your apps here to enable them
"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",
# the social providers
"allauth.socialaccount.providers.agave",
"allauth.socialaccount.providers.amazon",
"allauth.socialaccount.providers.angellist",
"allauth.socialaccount.providers.asana",
"allauth.socialaccount.providers.auth0",
"allauth.socialaccount.providers.authentiq",
"allauth.socialaccount.providers.baidu",
"allauth.socialaccount.providers.basecamp",
"allauth.socialaccount.providers.bitbucket",
"allauth.socialaccount.providers.bitbucket_oauth2",
"allauth.socialaccount.providers.bitly",
"allauth.socialaccount.providers.cern",
"allauth.socialaccount.providers.coinbase",
"allauth.socialaccount.providers.dataporten",
"allauth.socialaccount.providers.daum",
"allauth.socialaccount.providers.digitalocean",
"allauth.socialaccount.providers.discord",
"allauth.socialaccount.providers.disqus",
"allauth.socialaccount.providers.douban",
"allauth.socialaccount.providers.draugiem",
"allauth.socialaccount.providers.dropbox",
"allauth.socialaccount.providers.dwolla",
"allauth.socialaccount.providers.edmodo",
"allauth.socialaccount.providers.edx",
"allauth.socialaccount.providers.eveonline",
"allauth.socialaccount.providers.evernote",
"allauth.socialaccount.providers.exist",
"allauth.socialaccount.providers.facebook",
"allauth.socialaccount.providers.feedly",
"allauth.socialaccount.providers.fivehundredpx",
"allauth.socialaccount.providers.flickr",
"allauth.socialaccount.providers.foursquare",
"allauth.socialaccount.providers.fxa",
"allauth.socialaccount.providers.github",
"allauth.socialaccount.providers.gitlab",
"allauth.socialaccount.providers.google",
"allauth.socialaccount.providers.hubic",
"allauth.socialaccount.providers.instagram",
"allauth.socialaccount.providers.jupyterhub",
"allauth.socialaccount.providers.kakao",
"allauth.socialaccount.providers.keycloak",
"allauth.socialaccount.providers.line",
"allauth.socialaccount.providers.linkedin",
"allauth.socialaccount.providers.linkedin_oauth2",
"allauth.socialaccount.providers.mailru",
"allauth.socialaccount.providers.mailchimp",
"allauth.socialaccount.providers.meetup",
"allauth.socialaccount.providers.microsoft",
"allauth.socialaccount.providers.mixer",
"allauth.socialaccount.providers.naver",
"allauth.socialaccount.providers.nextcloud",
"allauth.socialaccount.providers.odnoklassniki",
"allauth.socialaccount.providers.openid",
"allauth.socialaccount.providers.openstreetmap",
"allauth.socialaccount.providers.orcid",
"allauth.socialaccount.providers.paypal",
"allauth.socialaccount.providers.patreon",
"allauth.socialaccount.providers.persona",
"allauth.socialaccount.providers.pinterest",
"allauth.socialaccount.providers.reddit",
"allauth.socialaccount.providers.robinhood",
"allauth.socialaccount.providers.sharefile",
"allauth.socialaccount.providers.shopify",
"allauth.socialaccount.providers.slack",
"allauth.socialaccount.providers.soundcloud",
"allauth.socialaccount.providers.spotify",
"allauth.socialaccount.providers.stackexchange",
"allauth.socialaccount.providers.steam",
"allauth.socialaccount.providers.strava",
"allauth.socialaccount.providers.stripe",
"allauth.socialaccount.providers.trello",
"allauth.socialaccount.providers.tumblr",
"allauth.socialaccount.providers.twentythreeandme",
"allauth.socialaccount.providers.twitch",
"allauth.socialaccount.providers.twitter",
"allauth.socialaccount.providers.untappd",
"allauth.socialaccount.providers.vimeo",
"allauth.socialaccount.providers.vimeo_oauth2",
"allauth.socialaccount.providers.vk",
"allauth.socialaccount.providers.weibo",
"allauth.socialaccount.providers.weixin",
"allauth.socialaccount.providers.windowslive",
"allauth.socialaccount.providers.xing",
"allauth.socialaccount.providers.yandex",
"allauth.socialaccount.providers.ynab",
),
SOCIALACCOUNT_PROVIDERS = {
'google': {
# For each OAuth based provider, either add a ``SocialApp``
# (``socialaccount`` app) containing the required client
# credentials, or list them here:
'APP': {
'client_id': '123',
'secret': '456',
'key': ''
}
}
}
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
'django.contrib.auth.hashers.SHA1PasswordHasher',
'django.contrib.auth.hashers.MD5PasswordHasher',
'django.contrib.auth.hashers.CryptPasswordHasher',
)
# Middleware framework
# https://docs.djangoproject.com/en/2.1/topics/http/middleware/
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
##'Website.middleware.current_user.CurrentUserMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Website.urls'
# Template configuration
# https://docs.djangoproject.com/en/2.1/topics/templates/
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
WSGI_APPLICATION = 'Website.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES={
'default':{
'ENGINE':'sql_server.pyodbc',
'NAME':'NKFSLTD_Website',
'USER':'Nf00038',
'PASSWORD':'hidden',
'HOST':'192.168.1.116',
'PORT':'1433',
'OPTIONS':{
'driver':'SQL Server Native Client 11.0',
},
}
}
# Some other example server values are
# server = 'localhost\sqlexpress' # for a named instance
# server = 'myserver,port' # to specify an alternate port
class SQLConnection():
def Connection():
server = '192.168.1.118'
database = 'NKFSLTD_Website'
username = 'sa'
password = 'Heron24$'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.1/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/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = ('G:\inetpub\FaulknerandSonsLTD2\Website\Website\Website\static\static_root')
STATICFILES_DIRS = (('G:\inetpub\FaulknerandSonsLTD2\Website\Website\Website\static\staticfiles'),)
STUDENTS_URL = '/studentsfiles/'
STUDENTS_ROOT = ('I:\Students')
STUDENTFILES_DIRS = ('I:\Students')
PARENTS_URL = '/parentsfiles/'
PARENTS_ROOT = ('H:\Parents')
PARENTFILES_DIRS = ('H:\Parents')
TEACHERS_URL = '/teachersfiles/'
TEACHERS_ROOT = ('C:\Teachers')
TEACHERSFILES_DIRS = ('C:\Teachers')
MEDIA_URL = '/media/'
#MEDIA_ROOT = ('G:\inetpub\FaulknerandSonsLTD2\Website\Website\Website\media\media_root')
MEDIA_ROOT = os.path.join(BASE_DIR, 'firstapp/static/')
# django registration
ACCCOUNT_ACTIVATION_DAYS = 7
INSTALLED_APPS 應該看起來像字符串元組:
INSTALLED_APPS = ("app1", "app2")
但是您在其中的某處有列表,即:
INSTALLED_APPS = ("app1", ["app2"])
檢查https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
在:
File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\site-packages\django\__init__.py",
> line 24, in setup
你可以找到:
apps.populate(settings.INSTALLED_APPS)
並且在
> File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\site-packages\django\apps\registry.py",
> line 91, in populate
你可以找到:
> app_config = AppConfig.create(entry)
並在:
> File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\site-packages\django\apps\config.py",
> line 90, in create
你可以找到:
> module = import_module(entry)
並在:
> File "G:\Program Files (x86)\Microsoft Visual Studio\2019\Shared\Python37_64\lib\importlib\__init__.py", line 118,
> in import_module
你可以找到:
> if name.startswith('.'):
但是 settings.INSTALLED_APPS 看起來不錯,你確定你還有這個錯誤嗎
當我在 INSTALLED_APPS 的定義之后添加“,”時,我能夠重現該問題
AttributeError: 'tuple' object has no attribute 'startswith'
請更換
INSTALLED_APPS = (),
至
INSTALLED_APPS = ()
只是想添加我的解決方案,因為我也偶然發現了這個問題。 它與代碼中的任何錯誤無關,但與 python 系統設置有關。 盡管沒有代碼更改,但能夠重現一個分支有錯誤而另一個沒有。 通過切換回系統 python 進行修復。 以前,由於升級 python 時出現類似錯誤,我已移至較低的 python。 但是當我讓 brew 和 pyenv 見面時似乎已經修復了。 所以他們之前沒有鏈接可能至少是我問題的根源,導致 py 和依賴不匹配? 盡管沒有代碼錯誤,但最終導致錯誤。
您在此處的元組中創建了一個元組
INSTALLED_APPS = (
"app",
"Website",
#some more apps
"allauth.socialaccount.providers.ynab",
),
刪除字符串我們得到INSTALLED_APPS = (),
這與INSTALLED_APPS = ((), ())
相同。
根據文檔,您應該有一個列表,例如
INSTALLED_APPS = [
"app",
"Website",
#some more apps
"allauth.socialaccount.providers.ynab",
]
注意最后一行沒有逗號。
刪除字符串,我們現在得到INSTALLED_APPS = []
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.