简体   繁体   English

在Javascript中使用Django常数

[英]Using Django constants in Javascript

I'm trying to update a field in the database by calling a url pattern from urls.py. 我试图通过从urls.py调用url模式来更新数据库中的字段。 The pattern is defined as follows (code slightly changed for security): 该模式定义如下(出于安全性考虑,代码略有更改):

(r'^pref/(?P<key>\\d+)/(?P<value>\\d+)/$', 'client.views.update_pref'),

key: ENABLE_T (int value 7 as defined in constants.py) 键:ENABLE_T(int值7,在constants.py中定义)

value: 0 (boolean int for False) 值:0(布尔型int为False)

In Javascript, I've attempted to call /pref/ENABLE_T/0 under this block of code, but it doesn't go into the $.ajax() function at all. 在Javascript中,我试图在此代码块下调用/ pref / ENABLE_T / 0,但它根本没有加入$.ajax()函数。

$.ajax('/pref/7/0/', {
    dataType: 'json',
    success: function(data) {
        alert("Success");
    },
    error: function() {
        // TODO: handle error
        alert("Preferences Error");
    }
});

I'd rather use ENABLE_T instead of the 7 to call that constant. 我宁愿使用ENABLE_T而不是7来调用该常量。 Any suggestions? 有什么建议么? Thanks! 谢谢!

That depends on where that JS is: 这取决于JS在哪里:

Embedded in a page's HTML: 嵌入页面的HTML中:

You can get away with pref/{{ ENABLE_T}}/0/ provided that you pass ENABLE_T in the context (see below). 只要您在上下文中传递ENABLE_T (请参阅下文),就可以使用pref/{{ ENABLE_T}}/0/

In an external JS file 在外部JS文件中

In your HTML, define: 在您的HTML中,定义:

<script>
    window.enable_t = '{{ ENABLE_T }}';
</script>

In your JS file, use 在您的JS文件中,使用

'pref/'+ window.enable_t + '/0/'

You'll also need to pass ENABLE_T to the context. 您还需要将ENABLE_T传递给上下文。

How to pass ENABLE_T to the context 如何将ENABLE_T传递给上下文

Use a Context Processor . 使用上下文处理器 You'll need to define the processor, and add it to the TEMPLATE_CONTEXT_PROCESSORS setting, as shown in the docs 您需要定义处理器,并将其添加到TEMPLATE_CONTEXT_PROCESSORS设置中,如文档所示

Why use a Context Processor? 为什么要使用上下文处理器?

You may also pass ENABLE_T directly to the context, in your view. 您也可以在视图中将ENABLE_T直接传递给上下文。 However, this means that you have to do it in every view . 但是,这意味着您必须在每个视图要做。

A context processor lets you avoid code duplication by automatically adding the constant to your contexts, whenever you use RequestContext - which you probably won't even have to think about if you're using Class Based Views. 上下文处理器使您可以在每次使用RequestContext时自动将常量添加到上下文中,从而避免代码重复-如果您使用的是基于类的视图,则甚至不必考虑它。

You have several choices. 您有几种选择。 Firstly, you can render javascript file as template so you can inject template variables to javascript. 首先,您可以将javascript文件渲染为模板,以便将模板变量注入javascript。 You can have django template like: 您可以使用django模板,例如:

var my_variable="{{DJANGO_VARIABLE}}";
//do something with variable
.

Another choice is to render only variables and pass them for example by ajax on hardcoded url, or include variables in html template and at the and include javascript which operate on well defined variables. 另一个选择是仅呈现变量,并例如通过ajax在硬编码的url上传递变量,或者在html模板中的和处包含变量,并在可定义的变量上包含javascript。 For example you define variable X and in script which you load you use this variable. 例如,您定义变量X,并在加载的脚本中使用此变量。

Yet another option is to use django javascript integration scripts (look at the django-admin scripts). 另一个选择是使用django javascript集成脚本(请参阅django-admin脚本)。 There are some javascript functions defined to get for example some static variables or to reverse urls. 定义了一些javascript函数,以获取例如一些静态变量或反向url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM