Django - collectstatic 后出现 500 内部服务器错误
Posted
技术标签:
【中文标题】Django - collectstatic 后出现 500 内部服务器错误【英文标题】:Django - 500 internal server error after a collectstatic 【发布时间】:2019-07-19 19:52:54 【问题描述】:尝试在生产模式下切换(setting.py 中的 debug=False),我启动了 python manage.py collectstatic 命令。从我尝试连接到该站点的那一刻起,即使我通过 python manage.py runserver 0.0.0.0:8000 启动命令,它也会给我一个内部错误 500
这是我的设置.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.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'Game/template/game')
MEDIA_DIR = os.path.join(BASE_DIR,'game/media')
# 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 = '**************************************'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['<server-ip>','<server-domain>',]
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Accounts',
'Game',
'marketing',
'Blog',
'contactus',
'django_filters',
'widget_tweaks',
'debug_toolbar',
'tempus_dominus',
'bootstrap3_datetime',
'django_summernote',
'session_security',
]
SUMMERNOTE_THEME = 'bs4'
#SESSION SECURITY SETTINGS
SESSION_SECURITY_WARN_AFTER = 540
SESSION_SECURITY_EXPIRE_AFTER = 1800
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
MIDDLEWARE = [
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'session_security.middleware.SessionSecurityMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
ROOT_URLCONF = 'CicloPost.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 = 'CicloPost.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
# 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 = 'it-IT'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = False
# Static files (CSS, javascript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
#MEDIA
MEDIA_ROOT =os.path.join(BASE_DIR,'game/media')
MEDIA_URL = '/media/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL= '/'
INTERNAL_IPS = ('127.0.0.1',)
当我做collectstatic时我也有这个错误:
后处理 'js/plugin/revolution/css/settings.css' 失败!
Traceback(最近一次调用最后一次):文件“manage.py”,第 15 行,在 execute_from_command_line(sys.argv) 文件 "/opt/ciclo_proj/CicloPost/ciclo_env/lib/python3.6/site-packages/django/core/management/init.py", 第 381 行,在 execute_from_command_line utility.execute() 文件“/opt/ciclo_proj/CicloPost/ciclo_env/lib/python3.6/site-packages/django/core/management/init.py”, 第 375 行,执行中 self.fetch_command(子命令).run_from_argv(self.argv)文件“/opt/ciclo_proj/CicloPost/ciclo_env/lib/python3.6/site-packages/django/core/management/base.py”, 第 316 行,在 run_from_argv self.execute(*args, **cmd_options) 文件“/opt/ciclo_proj/CicloPost/ciclo_env/lib/python3.6/site-packages/django/core/management/base.py”, 第 353 行,执行中 输出 = self.handle(*args, **options) 文件“/opt/ciclo_proj/CicloPost/ciclo_env/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py”, 第 188 行,在句柄中 收集= self.collect()文件“/opt/ciclo_proj/CicloPost/ciclo_env/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py”, 第 134 行,收集中 提高处理文件“/opt/ciclo_proj/CicloPost/ciclo_env/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py”, 第 293 行,在 _post_process 内容=模式.sub(转换器,内容)文件“/opt/ciclo_proj/CicloPost/ciclo_env/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py”, 第 194 行,在转换器中 force=True,hashed_files=hashed_files,文件“/opt/ciclo_proj/CicloPost/ciclo_env/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py”, 第 131 行,在 _url hashed_name = hashed_name_func(*args) 文件“/opt/ciclo_proj/CicloPost/ciclo_env/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py”, 第 342 行,在 _stored_name 中 cache_name = self.clean_name(self.hashed_name(name)) 文件“/opt/ciclo_proj/CicloPost/ciclo_env/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py”, 第 92 行,在 hashed_name 中 raise ValueError("无法使用 %r 找到文件 '%s'。" % (filename, self)) ValueError: The file 'js/plugin/revolution/css/openhand.cur' 找不到 .
【问题讨论】:
使用 Debug=False 启动服务器时是否收到同样的错误? 不,如果 Debug=False 我没有任何错误 究竟是什么导致了异常?静电收集?一些 URL 访问尝试?你可以分享堆栈跟踪吗? 我更新了帖子 请澄清您的问题:您何时收到此错误,运行什么命令?您是否在 prodin production mode (debug=False in setting.py), I launched
或 dev no if Debug=False i don't have any error
模式下出错?从您的描述中不清楚,并且您的一些断言相互冲突。
【参考方案1】:
当您设置DEBUG = False
时,您需要有一个网络服务器来侦听settings.STATIC_URL
中指定的url 上的端口80,并从settings.STATIC_ROOT
中指定的目录中提供文件。
我建议不要将settings.STATIC_ROOT
相对于BASE_DIR
使用(这可能会让人感到困惑,通常您需要来自同一个静态网络服务器提供的多个项目的静态文件)。
【讨论】:
以上是关于Django - collectstatic 后出现 500 内部服务器错误的主要内容,如果未能解决你的问题,请参考以下文章
Django collectstatic 没有这样的文件或目录
Django,即使在 collectstatic 之后也没有提供 uwsgi 静态文件