9

I'm encountering a very strange error. I have an app ID defined in my settings.py file like so:

CARDSPRING_APP_ID = '################'

This works on nearly every page in my site, except for one. Strangely enough, other variables work. In a script section on the page, I have the following:

alert("cs appid=" + {{ CARDSPRING_APP_ID }} + 
" sectoken=" + {{ securityToken }} + 
" timestamp= " +{{ timestamp }} + 
" hash = " + {{ digestedHash }} + 
" ccnum " + $('.card-number').val() + 
" exp" + $('.expiration-month').val() + $('.expiration-year').val() + 
" user = " + {{ csid }});

When the page is rendered, it evaluates to this

alert("cs appid=" +  + 
" sectoken=" + DDFJRMZXD12WVWHFFC###### + 
" timestamp= " +1346183125 + 
" hash = " + a929b3aec9179c700c09d###### + 
" ccnum " + $('.card-number').val() + 
" exp" + $('.expiration-month').val() + $('.expiration-year').val() + 
" user = " + SG1###);

Importantly, {{ CARDSPRING_APP_ID }} has evaluated to nothing. Does anyone know why this might be the case? Thank you!

UPDATE

I tried creating a context_processors.py file as described in the answer below, and made sure to add it to the appropriate spot in settings.py . I still don't have any luck -- it evaluates on one page, but not on the other

UPDATE 2

The template is called with this command:

return render_to_response('howto'+str(number)+'.html',locals(),context_instance= RequestContext(request))

UPDATE 3 Got it to work -- needed to add this to my settings.py

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    "myapp.context_processors.cardspring",
)
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
mythander889
  • 915
  • 5
  • 16
  • 23
  • 1
    Do you have a context processor that gives `CARDSPRING_APP_ID` its value in templates? – Simeon Visser Aug 28 '12 at 19:53
  • I'm not sure -- how would I check? I know that `CARDSPRING_APP_ID` evaluates appropriately on other pages in the site, just not on this one – mythander889 Aug 28 '12 at 19:57
  • How do you pass `CARDSPRING_APP_ID` to those pages then? It may be better to add it as a context processor anyway as that adds it automatically to all pages. – Simeon Visser Aug 28 '12 at 20:17
  • @Simeon Visser That's what I'm not sure of -- in the past, all I had to do was define it in settings.py and it'd automatically be accessible in my templates. Is there anything I need to add to the html itself to make sure it looks in settings.py (or in the context processsors, for that matter)? – mythander889 Aug 28 '12 at 20:19
  • Variables from `settings.py` aren't added automaticly to templates context. – Konrad Hałas Aug 28 '12 at 20:20

1 Answers1

11

Create a file called context_processors.py and write the following context processor:

from django.conf import settings

def cardspring(request):
    return { 'CARDSPRING_APP_ID': settings.CARDSPRING_APP_ID }

Then add your.location.context_processors.cardspring to TEMPLATE_CONTEXT_PROCESSORS in your Django settings file, where your.location is the location of your context_processors.py file.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • 1
    I'm looking in `settings.py` now, but don't see `TEMPLATE_CONTEXT_PROCESSORS'. Should I add this? – mythander889 Aug 28 '12 at 20:02
  • @mythander889: Yes, in case it is not there then you should add it. Make sure you also add the default values (see https://docs.djangoproject.com/en/1.4/ref/settings/#template-context-processors ) and then add your own context processor. – Simeon Visser Aug 28 '12 at 20:16
  • I have added it as you described with a little help from [here](http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/), but still haven't had any luck. Is there something I have to include in the html file itself? That's the only thing I can think of that could be different – mythander889 Aug 28 '12 at 20:17
  • Nothing needs to be added to the HTML file but it could be that your view is not using the context processors. Are you using `render_to_response` to create the response in your view? – Simeon Visser Aug 28 '12 at 20:34
  • Yes -- I call it with this: return render_to_response('howto'+str(number)+'.html',locals(),context_instance= RequestContext(request)) – mythander889 Aug 28 '12 at 21:01
  • @mythander889: That's good because `RequestContext` calls the context processors. Then I'm not sure why the template variable has no value. Is there anything that you may have configured that causes your Django project to behave differently? – Simeon Visser Aug 28 '12 at 21:12
  • I got it to work -- the template context processors weren't loading correctly. Thank you! – mythander889 Aug 28 '12 at 22:26