Django将变量从Views.Py动态传递给Jquery [英] Django passing variables dynamically from Views.Py to Jquery
问题描述
我正在努力理解这一点.
I am struggling to understand this.
我有一本词典,标题是我从Django中的Views传递到HTML的寓言.然后,如何将字典引用到Jquery以自动填充HTML中的输入值?
I have a dictionary titled alldicts that I am passing from Views in Django into the HTML. How do I then reference that dictionary to Jquery to autofill in input values in my HTML?
我在Views.Py中的代码:
My code in Views.Py:
mydict1 = {
'one' : 1
'two' : 2
'three' : 3
}
mydict2 = {
'one' : 4
'two' : 5
'three' : 6
}
mydict3 = {
'one' : 7
'two' : 8
'three' : 9
}
alldicts={
'mydict1': mydict1,
'mydict2': mydict2,
'mydict3': mydict3
}
return render(request, self.template_name, alldicts)
在我的代码的HTML部分中,我具有一个带有选项"mydict1","mydict2"和"mydict3"的选择下拉列表.在它下面,我有三个输入(输入的数量将是动态的,但想举一个简单的例子),我想自动填充以匹配所选的选项.(即,如果我在下拉列表中选择mydict2,则输入(#one,#two和#three)将分别填充为4,5和6.)
In the HTML section of my code, I have a select dropdown with the options "mydict1","mydict2", and "mydict3". Below it I have three inputs (number of inputs will be dynamic, but wanted to give a simple example) that I want to auto fill to match the selected option. (IE if I select mydict2 in the dropdown, the inputs (#one, #two, and #three) will fill to be 4,5, and 6 respectively).
在html中,如果我尝试这样的操作,它将不起作用:
In html, if I try something like this, it doesn't work:
$("#hselect").change(function() {
var a = "{{alldicts}}";
var selectedValue = $(this).val();
$.each( a, function(idx, obj) {
$.each( obj, function(key, value){
if (selectedValue == idx) {
$('#'+key).val(value);
}
});
});
}
<select id = "hselect" name="hselect" style="width: 250px;" onchange="changeoption();">
<option> mydict1 </option>
<option> mydict2 </option>
<option> mydict3 </option>
</select>
<input id='one' ><br>
<input id='two' ><br>
<input id='three' ><br>
这不起作用.但是,如果我通过HTML静态传递字典,则它确实可以工作.如何从视图动态传递?
This does not work. However if I pass the dictionary statically through the HTML, it does work. How do I pass dynamically from Views?
例如,这确实有效:
$("#hselect").change(function() {
var a = {
'mydict1' = {'one' : 1, 'two' : 2, 'three' : 3},
'mydict2' = {'one' : 4, 'two' : 5, 'three' : 6},
'mydict3' = {'one' : 7, 'two' : 8, 'three' : 9},
};
var selectedValue = $(this).val();
$.each( a, function(idx, obj) {
$.each( obj, function(key, value){
if (selectedValue == idx) {
$('#'+key).val(value);
}
});
});
}
<select id = "hselect" name="hselect" style="width: 250px;" onchange="changeoption();">
<option> mydict1 </option>
<option> mydict2 </option>
<option> mydict3 </option>
</select>
<input id='one' ><br>
<input id='two' ><br>
<input id='three' ><br>
唯一改变的是我如何传递字典,mydicts和定义变量a.如何从Views.Py动态地做到这一点?
The only thing that changes is how I pass the dictionary, mydicts, and define the variable a. How do I do this dynamically from Views.Py?
推荐答案
-
您需要确保将
alldicts
中的所有数据类型从python对象
转换为string
或数字
.
将 views.py
中的 alldicts
从 python dict
更改为 json格式
:>
import json
json_alldicts = json.dumps(alldicts)
对于 jinja模板
var a = JSON.parse('{{ json_alldicts | tojson | safe }}');
console.log(a);
对于 django模板
var a = JSON.parse('{{ json_alldicts | safe }}');
console.log(a);
这篇关于Django将变量从Views.Py动态传递给Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!