djangostaticwhitenoise

whitenoise dosen't seem to work in Django


I want to check my web app in DEBUG = False and use whitenoise to do the static stuff. I have followed documentation and different blog post, I have also used the tool before, but this time it just does not work.

Things I've tried:

  1. reinstalling whitenoise
  2. changing the location of whitenoise middleware
  3. collected statics many times and also changed its name
  4. restart the local web server

settings.py:

DEBUG = False

ALLOWED_HOSTS = ['*']

MIDDLEWARE = [
    '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',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

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

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / "static"

urls.py:

urlpatterns = [
    path('', include('bella_beauty_shop_web_store.urls')),
    path('123321/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

and here you can see that I have collected statics: enter image description here

Errors: When I put the DEBUG to FALSE, there is an error in the console: enter image description here

I really don't know what's wrong here. I am quit new to Django, but I have built some web apps and also used whitenoise before.

As you know, I expect my html to render with style when the DEBUG is set to FALSE using whitenoise.


Solution

  • yeah finally I've found the problem. I was using the wrong form of adding static files in templates, like a syntax error but doesn't really rises any error.

    Instead of this src: url('../fonts/iran-sans/IRANSans-Medium.woff') format('woff'); I changed it to this src: url('fonts/iran-sans/IRANSans-Medium.woff') format('woff');

    Instead of this <link rel="stylesheet" type="text/css" href={% static 'style-index.css' %}/> I changed it to this <link rel="stylesheet" type="text/css" href="{% static 'style-index.css' %}"/> -> if you pay a close look you can see that in the first one I used href=, though in the second one I used href="".

    It looks kinda stupid but when the DEBUG is True, the first ones work great.