1

When I set DEBUG=False in my settings file in django 1.5, I no longer have access to the STATIC_URL or any of the other variables that should be loaded by my TEMPLATE_CONTEXT_PROCESSORS in my django templates. Oddly, everything works when DEBUG=True. For what its worth, I definitely have 'django.core.context_processors.static' in my TEMPLATE_CONTEXT_PROCESSORS so that is not the issue. I have also checked a few other variables in my template context and none of the other nothing seems to be there. MEDIA_URL? nope. request? nope. See this example on github (which has been updated with solution), but these are the important pieces that correctly work when DEBUG=True and throw a 500 error when DEBUG=False:

# settings.py
from django.conf.global_settings import *
# ...
TEMPLATE_CONTEXT_PROCESSORS += (
    'django.core.context_processors.request',
)
# believe it or not, 'django.core.context_processors.static' is in there
print TEMPLATE_CONTEXT_PROCESSORS 

# views.py
from django.template import RequestContext
from django.shortcuts import render_to_response
def wtf(request):
    return render_to_response(
        "wtf.html", {}, 
        context_instance=RequestContext(request)
    )

Does something special happen in django 1.5 when you turn off debug mode? Any suggestions for fixing and/or debugging the problem would be greatly appreciated!

dino
  • 3,093
  • 4
  • 31
  • 50

2 Answers2

0

Looks like there was a change between Django 1.2 and 1.3.

You now have to include django.core.context_processors.static in your TEMPLATE_CONTEXT_PROCESSORS if you want the STATIC_URL available to your template outside of debug mode.

You also need to ensure you're using a RequestContext instance when rendering the template.

Aya
  • 39,884
  • 6
  • 55
  • 55
  • thanks for the response, Aya. I clarified the question to make it clear that I am already using the RequestContext and that the django.core.context_processors.static is already in the TEMPLATE_CONTEXT_PROCESSORS. Anyway, this doesn't explain why *none* of the other variables are present either. Any ideas? – dino Apr 18 '13 at 14:11
0

This can be fixed by editing the ALLOWED_HOSTS variable in your settings.py. See this answer for more details.

To get this to work on localhost, for example, set ALLOWED_HOSTS = ['localhost'].

Community
  • 1
  • 1
dino
  • 3,093
  • 4
  • 31
  • 50