Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django settings.py variables in templates

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",
)
like image 957
mythander889 Avatar asked Aug 28 '12 19:08

mythander889


People also ask

Can I access constants in settings py from templates in Django?

"Django provides access to certain, frequently-used settings constants to the template such as settings.

Can we create a variable in Django template?

We can set the value of a variable in the Django template using with tag. This will output the below content. One downside of this approach is that we have to write the lines where we are accessing the variable inside with and endwith block. Using with is useful when using a costly variable multiple times.

What does {{ name }} this mean in Django templates?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.


1 Answers

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.

like image 107
Simeon Visser Avatar answered Sep 19 '22 23:09

Simeon Visser