Liam Hanninen
Liam Hanninen

Reputation: 1573

How to stop Django from serving files locally?

I need to prevent local Django/runserver from serving my static files (I want to test whitenoise locally). The --nostatic flag is supposed to do just that.

python manage.py runserver --nostatic

But I still get 200s (successful) for my static files. So then I set debug = False but still get 200s! I even commented out 'django.contrib.staticfiles' from INSTALLED_APPS. But this did not work either. How could my static files still be served successfully - it's usually not this hard to break things. Perhaps I am misunderstanding what nostatic actually does; I was expecting to get 404s.

Upvotes: 0

Views: 178

Answers (2)

First, clear browser's cache because your browser will still keep the cache of the static files of Django . *On Google Chrome, you can open the page to clear cache with Ctrl+Shift+Del which also works on Firefox. Or you can empty cache and reload the page with Ctrl+F5, Shift+F5 or Ctrl+Shift+R (Ctrl+F5 or Ctrl+Shift+R in Firefox). Or you can use the Google Chrome extension Clear Cache to clear cache.

And according to my experiments, if I set WhiteNoiseMiddleware of whitenoise to MIDDLEWARE in settings.py and run the Django server with --nostatic as shown below, the static files are still served:

# "settings.py"

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', # Here
    'django.contrib.sessions.middleware.SessionMiddleware',
    ...
]
python manage.py runserver --nostatic

And according to my experiments, if I don't set WhiteNoiseMiddleware of whitenoise to MIDDLEWARE in settings.py and run the Django server with --nostatic as shown below, the static files are not served:

# "settings.py"

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    # 'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    ...
]
python manage.py runserver --nostatic

Upvotes: 0

D. Evans
D. Evans

Reputation: 3292

Resolved in the comments, but for future reference the answer was to make sure that whitenoise.middleware.WhiteNoiseMiddleware was removed from the MIDDLEWARE list.

Upvotes: 1

Related Questions