Ubuntu部署rlcard-showdown遇Django ROOT_URLCONF属性错误求助
Django运行rlcard-showdown自定义域名时出现AttributeError问题解决
环境信息
- 服务器系统:Ubuntu
- 依赖框架:rlcard、rlcard-showdown
- Django版本:4.2.3
- Node.js版本:20.4.0
- 问题场景:使用自定义域名访问rlcard-showdown时触发500错误
报错信息
Traceback (most recent call last): File "/usr/lib/python3.8/wsgiref/handlers.py", line 137, in run self.result = application(self.environ, self.start_response) File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/wsgi.py", line 124, in __call__ response = self.get_response(request) File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py", line 139, in get_response set_urlconf(settings.ROOT_URLCONF) File "/usr/local/lib/python3.8/dist-packages/django/conf/__init__.py", line 104, in __getattr__ val = getattr(_wrapped, name) File "/usr/local/lib/python3.8/dist-packages/django/conf/__init__.py", line 311, in __getattr__ return getattr(self.default_settings, name) AttributeError: module 'django.conf.global_settings' has no attribute 'ROOT_URLCONF' [06/Jul/2023 17:13:06] "GET / HTTP/1.1" 500 59 Traceback (most recent call last): File "/usr/lib/python3.8/wsgiref/handlers.py", line 137, in run self.result = application(self.environ, self.start_response) File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/wsgi.py", line 124, in __call__ response = self.get_response(request) File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py", line 139, in get_response set_urlconf(settings.ROOT_URLCONF) File "/usr/local/lib/python3.8/dist-packages/django/conf/__init__.py", line 104, in __getattr__ val = getattr(_wrapped, name) File "/usr/local/lib/python3.8/dist-packages/django/conf/__init__.py", line 311, in __getattr__ return getattr(self.default_settings, name) AttributeError: module 'django.conf.global_settings' has no attribute 'ROOT_URLCONF' [06/Jul/2023 17:13:06] "GET /favicon.ico HTTP/1.1" 500 59
当前settings.py配置
import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '-t@mf4fi)gfxzv5lm8qkg)*5u^brj--y*ul2&ryqdem(xin8(!' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = TRUE ALLOWED_HOSTS = ['mydomain(i put this)', '000.000.00.00', '000.000.00.00'] # Cors config CORS_ORIGIN_ALLOW_ALL=True # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'corsheaders', 'tournament', ] MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', '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 = 'server.urls' 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 = 'server.wsgi.application' # Database # https://docs.djangoproject.com/en/2.2/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } } # Password validation # https://docs.djangoproject.com/en/2.2/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.2/howto/static-files/ 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.2/howto/static-files/ STATIC_URL = '/static/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
问题分析与修复步骤
1. 语法错误定位
settings.py存在两处关键错误:
- DATABASES配置多了闭合大括号:在DATABASES的default配置结束后,多写了一个
},导致整个settings模块加载失败。 - DEBUG值错误:Python布尔值应为小写
True,而非大写TRUE,这也会导致配置解析异常。
当settings模块加载失败时,Django会尝试使用默认的global_settings,但ROOT_URLCONF是项目自定义配置,global_settings中不存在该属性,因此触发AttributeError。
2. 修复配置
修改settings.py中的两处错误:
# 修复DEBUG值 DEBUG = True # 修复DATABASES语法错误 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }
3. 其他注意事项
- 将
ALLOWED_HOSTS中的占位符替换为实际的自定义域名和服务器公网IP,确保格式正确(例如['yourdomain.com', '1.2.3.4'])。 - 生产环境下建议关闭
DEBUG模式,配置合适的CORS_ORIGIN_WHITELIST替代CORS_ORIGIN_ALLOW_ALL,提升安全性。
4. 验证修复
保存修改后,重启Django服务,使用自定义域名访问,检查是否正常响应。
内容的提问来源于stack exchange,提问作者김갓산이
相关产品推荐
相关产品推荐

