django settings.py DEBUG=Falseについて
Debug=False にすると静的ファイルが読み込めない CSS とか JS
なので少し調べてみた
Whitenoiseを使う方法
pip install whitenoise
この後 setting.py に記入
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'whitenoise',
]
MIDDLEWARE = [
'xxxxxxxxx',
'whitenoise.middleware.WhiteNoiseMiddleware',
].
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'collectstatic')
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
STATIC_DIR,
)
DEBUG=False に編集後 python manage.py collectstatic とする
これで静的ファイルが読めるらしい
参考サイト
別の方法(公式かはわかりませんがうまく動きました)
import re
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
from django.views.static import serve
static_patterns = [
re_path(r"^%s(?P<path>.*)$" % re.escape(settings.STATIC_URL.lstrip("/")),
serve,
{'document_root': settings.STATIC_ROOT}),
re_path(r"^%s(?P<path>.*)$" % re.escape(settings.MEDIA_URL.lstrip("/")),
serve,
{'document_root': settings.MEDIA_ROOT}),
] if not settings.DEBUG else []
urlpatterns = static_patterns + [
path('admin/', admin.site.urls),
path('xxxxx' xxxx),
]