I have custom middleware, when I add it to my MIDDLEWARE_CLASSES tuple in my settings.py all of the css and images on the site disappear leaving the site with just text.
middleware.py
from django.shortcuts import redirect
from django.core import urlresolvers
class AcceptTOSMiddleware(object):
def process_request(self, request):
login_url = urlresolvers.reverse('login')
tos_url = urlresolvers.reverse('tos')
if request.path not in [login_url, tos_url]:
profile = request.user.get_profile()
if not profile.tos:
return redirect(tos_url)
return None
settings.py
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'pagination.middleware.PaginationMiddleware',
'utils.middleware.AcceptTOSMiddleware'
)
Does anyone know why this would happen?
This should only happen in development, otherwise the HTTPD would serve the static files and there would be nothing on django’s side to block it,
Read runserver’s console output, it should show that requests for static files are responded with a 3xx redirect (probably 301),
For development, your middleware should also check if
request.pathdoesn’t start withsettings.STATIC_URLnorsettings.MEDIA_URL, andreturn Nonein that case.