2

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)
cvsrt
  • 357
  • 4
  • 19
  • 1
    Did you specify media/static urls in the `urls.py`? – Willem Van Onsem Sep 06 '21 at 15:57
  • You can also add this `whitenoise.runserver_nostatic` in installed apps – Raja Simon Sep 06 '21 at 15:59
  • @WillemVanOnsem my urls.py file like this: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) – cvsrt Sep 06 '21 at 16:03

2 Answers2

3

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)
Shreyash mishra
  • 738
  • 1
  • 7
  • 30
  • for using `staticfiles_urlpatterns`: **this only works when `DEBUG=True` in `settings.py` which means you should NOT use it for production environment.** ref https://docs.djangoproject.com/en/2.0/ref/contrib/staticfiles/#django.contrib.staticfiles.urls.staticfiles_urlpatterns – Wei Jing May 17 '23 at 02:03
3

To use WhiteNoise with django :

  1. Make sure staticfiles is configured correctly

In the project settings.py file set

STATIC_ROOT = BASE_DIR / 'staticfiles' This staticfiles folder will be the folder where collectstatic will pull out all your static files.

  1. Enable WhiteNoise Always in settings.py:

    MIDDLEWARE = [

     'django.middleware.security.SecurityMiddleware',
    
     'whitenoise.middleware.WhiteNoiseMiddleware',
     # ...
    

    ]

NB : The WhiteNoise middleware should be placed directly after the Django SecurityMiddleware (if you are using it) and before all other middleware.

That is all, but if you want more performance you should enable caching and compression support like this :

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

If you want to apply compression but don’t want the caching behaviour then you can use:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

Follow these steps and all will be ok. WhiteNoise Documentation

Rvector
  • 2,312
  • 1
  • 8
  • 17