2012-05-11 19 views
0

私は、とりわけ気候パラメータを選択するための2つのフィールドを含むdjangoフォームを持っています。これらのフィールドの両方に同じ選択肢が設定されていますが、最初に選択されたものの影響を受けるために必要なものが必要でした。基本的に、最初にパラメータが選択された場合は、2番目のパラメータの選択肢から削除する必要があります。Djangoはjavascriptで動的フォームの言語を取得します

これで、1つの問題を除いて、これで非常にうまくいくコードができました。私は言語認識の分野が必要です。

forms.py

class ClimateForm(forms.Form): 
    ... 
    param1 = forms.ChoiceField(choices=PARAM_CHOICES, label=_('Y axis one'), initial=_('Not Specified'), 
       widget=forms.Select(attrs={"onChange":'activateParam()'}), 
       ) 
    param2 = forms.ChoiceField(choices=PARAM_CHOICES, label=_('Y axis two'), initial=_('Not Specified'), 
       required = False, 
       ) 
    ... 

私は、これを行うactivateParam()関数を呼び出す:

filter_form.js

function activateParam() { 
    // get value to exclude from select 
    var param1Val = document.getElementById("id_param1").value; 
    // get next element id 
    var param2 = document.getElementById("id_param2"); 
    // param2.setAttribute("style", "display:block"); 
    updateSelect($('select[name=param2]'), param1Val); 
} 


function updateSelect(select, value) { 
    // find and remove existing options 
    select.find('option').remove(); 
    // loop through results and append to select as options 
    for (var k in climate_params) { 
     if (k!=value) { 
      select.append($('<option value="'+k+'">'+climate_params[k]+'</option>')); 
     } 
    } 
} 

climate_paramsの値の配列だけです2番目のフィールドに入力する文字列:

この配列を変更したり別のものを組み込んだりすることができましたが、このJSスクリプトに言語状態を渡す方法を知る必要があります。 JSで言語変数に直接アクセスできる方法はありますか?

ご迷惑をおかけして申し訳ございません。

答えて

0

あなたは可能性がmark translation string in js directly
やPythonの翻訳オブジェクトのw/climate_paramsを初期化します。

{# in template #} 
var climate_params = { 
    {% for param, label in PARAM_CHOICES %} 
    '{{ param|escapejs }}: '{{ label|escapejs }}', 
    {% endfor %} 
}; 

もチェックHow i get the current language in django?を見てみましょう。 Ajaxでラベルを渡している場合は、すでに翻訳が行われています。

関連する問題