我已转而模板常规的沙里文字典,我需要内部。
<代码>$(文件).ready(功能)({>}。
将这名暴徒改写成 Java字典。 我试图这样做。
var js_dict={{parameters}};
but I got errors ( instead of and all strings start with u ). How can I convert Python dictionary to JavaScript hash table ?
我已转而模板常规的沙里文字典,我需要内部。
<代码>$(文件).ready(功能)({>}。
将这名暴徒改写成 Java字典。 我试图这样做。
var js_dict={{parameters}};
but I got errors ( instead of and all strings start with u ). How can I convert Python dictionary to JavaScript hash table ?
粉碎和 j都对如何代表字典有不同的想法,这意味着你需要中间代表才能通过数据。 最常见的办法是,这是一个简单的轻重数据交换格式。
利用python json Library将(或丢弃)你的字塔改为JSON string。 然后,在“javascript”中,将“JSON”str成“javascript”。 (如果你正在使用JQuery,则使用。
You could convert it to JSON and use that in that template
您的法典
import json
...
...
return { parameters : json.dumps(parameters)} #This data goes into your template
您可使用<代码>json.dumps(参数),mark_safe(
。
def custom_view(request):
...
return render(request, tmpl.html , { parameters : mark_safe(json.dumps(parameters))})
在模板中,有<代码>mark_safe()。
我发现,这也有助于扼杀[ ]。
const convertPythonDictToJSON = function (data) {
let d = data.replace(new RegExp(`(?<=[a-zA-Z]) (?=[a-zA-Z ])`, "g"), __ )
d = d.replace(new RegExp(" ", g ), " )
d = d.replace(new RegExp("__", g ), " ")
d = d.replace(new RegExp("None", g ), null )
d = d.replace(new RegExp("False", g ), false )
d = d.replace(new RegExp("True", g ), true )
return JSON.parse(d)
}
正如其他人已经建议的那样,将你的字典转换为JSON,然后将JSON转至模板背景,这确实是你的最佳办法,但如果你想把它作为你的模板中的字典,你可以这样做;你只是需要人工制作 j版(即,你可以只把它放在<>{参数>><>>>>><>>>>>>><>>>>>>>>>><>>>>>>>>>>>>>>>>><>>>>>>>>>><>>>>>>>><>>>>>>>>>>>>>>>>>><>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<script>
var js_dict = {
{% for k, v in parameters %}
"{{ k }}": "{{ v }}"{% if not forloop.last %},{% endif %}
{% endfor %}
}
</script>
下面由Chris Patt回答(注视差异)启发的一个工作例子:
42 var js_dict = [
43 {% for k, v in parameters.iteritems %}
44 ["{{ k }}","{{ v }}"] {% if not forloop.last %},{% endif %}
45 {% endfor %}
46 ];
我在处理Django模板中的变数时,似乎不为我工作。
你们不需要寄出独裁者。
在这方面,我是如何落实的:
<>views.py
def mess_opi(request):
data = Opi_calculated.objects.all()
return render(request, hab_app/opi_student_portal.html , { data : data})
www.un.org/Depts/DGACM/index_russian.htm
data: [{% for item in data %}
{ "y": {{ item.hostelName }} , "a": {{ item.opi_value }} }{% if not forloop.last %},{% endif %}
{% endfor %}],
How handled this case by passing object on template page instead on passing the dict from py file and i created the required dict on template itself. like:
在我的发言中:
def abc(request):
conf = assessment_master.objects.all()
return render(request, "enter_scheme.html", { conf :conf})
on my django template: within my script tag:
<script>
var conf_dict_list = {};
{% for obj in conf_dict_list %}
conf_dict_list["{{ obj.short_name }}"] = "{{ obj.select }}"
{% endfor %}
alert(JSON.stringify(conf_dict_list));
// alert( key in conf_dict_list); to check key is present in dict
</script>
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...