Django settings.py变量在模板中 [英] Django settings.py variables in templates
问题描述
settings.py
文件中定义了一个应用程序ID: CARDSPRING_APP_ID ='################'
这个工作几乎在我网站上的每个页面,除了一个。奇怪的是,其他变量可以工作。在页面的脚本部分,我有以下内容:
alert(cs appid =+ {{CARDSPRING_APP_ID}} +
sectoken =+ {{securityToken}} +
timestamp =+ {{timestamp}} +
哈希=+ {{digestedHash}} +
ccnum+ $('。card-number')。val()+
exp+ $(',expiration-month')。val()+ $(',expiration-year')。 val()+
user =+ {{csid}});
当页面呈现时,它评估为
alert(cs appid =+ +
sectoken =+ DDFJRMZXD12WVWHFFC ###### +
timestamp =+1346183125 +
hash =+ a929b3aec9179c700c09d ###### +
ccnum+ $('。card-number')。val()+
exp+ $ 。val()+ $('。expiration-year')。val()+
user =+ SG1 ###)
重要的是, {{CARDSPRING_APP_ID}}
被评估为无。有人知道为什么会这样吗?谢谢!
更新
我尝试创建一个 context_processors.py
文件,并确保将其添加到 settings.py
中的相应位置。我仍然没有任何运气 - 它评估在一个页面,但不是在另一个
更新2
使用此命令调用模板:
返回render_to_response('howto'+ str (number)+'。html',locals(),context_instance = RequestContext(request))
strong>更新3
将其工作 - 需要将其添加到我的settings.py
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS +(
myapp.context_processors.cardspring,
)
创建一个名为 context_processors.py
的文件,并编写以下上下文处理器:
从django.conf导入设置
def cardspring(request):
return {'CARDSPRING_APP_ID':settings.CARDSPRING_APP_ID}
然后添加 your.locati您的Django设置文件中的on.context_processors.cardspring
至 TEMPLATE_CONTEXT_PROCESSORS
,其中 your.location
是您的 context_processors.py
文件的位置。
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",
)
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.
这篇关于Django settings.py变量在模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!