模板中的 Django settings.py 变量 [英] Django settings.py variables in templates

查看:38
本文介绍了模板中的 Django settings.py 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪的错误.我在 settings.py 文件中定义了一个应用 ID,如下所示:

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###);

重要的是,{{CARDSPRING_APP_ID }} 的评估结果为零.有谁知道为什么会这样?谢谢!

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

更新

我尝试创建一个 context_processors.py 文件,如下面的答案所述,并确保将其添加到 settings.py 中的适当位置.我仍然没有任何运气 - 它在一页上进行评估,但不在另一页上进行评估

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

更新 2

使用以下命令调用模板:

The template is called with this command:

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

更新 3让它工作 - 需要将它添加到我的 settings.py

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",
)

推荐答案

创建一个名为 context_processors.py 的文件并编写以下上下文处理器:

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 }

然后将 your.location.context_processors.cardspring 添加到 Django 设置文件中的 TEMPLATE_CONTEXT_PROCESSORS,其中 your.location 是您的位置context_processors.py 文件.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆