djangodjango-rest-frameworkwhitenoise

In Django, whitenoise do not show static files?


I am trying to show static files while debug=false.I am using whitenoise library but still website don't show the static files and media files. My settings.py file like this:

MIDDLEWARE = [
    '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',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [

    os.path.join(BASE_DIR, "static")
]


MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'


LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'

Urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
   path('',include('games.urls')),
  path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
    path('sentry-debug/', trigger_error),
    path('ajax_calls/search/', autocompleteModel),
   path('accounts/', include('allauth.urls')),
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

Solution

  • did you collect your static folder

    python manage.py collectstatic
    

    and in your main projects urls.py

     url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
        url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
    ]
    if settings.DEBUG:
        urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)