Django For循环到模板中的js对象

Django For循环到模板中的js对象,django,Django,我希望迭代从django视图返回的对象,以便在表中动态创建列(每列都是模型中的一个字段,我希望能够在不更改模板的情况下切换模型) 在模板的html主体部分,同样的代码运行良好,而在脚本部分则不然 我猜zip对象作为迭代器传递,只能使用一次?如果是这样,我如何让django发送一个普通的列表对象 我的看法是: def HelperColumnesFieldsAndNames(data): columns = { '<ManyToOneRel: client.paymentdata&g

我希望迭代从django视图返回的对象,以便在表中动态创建列(每列都是模型中的一个字段,我希望能够在不更改模板的情况下切换模型)

在模板的html主体部分,同样的代码运行良好,而在脚本部分则不然

我猜zip对象作为迭代器传递,只能使用一次?如果是这样,我如何让django发送一个普通的列表对象

我的看法是:

def HelperColumnesFieldsAndNames(data):
    columns = { '<ManyToOneRel: client.paymentdata>':["unimp_pay","unimplemented"],
'<ManyToOneRel: client.subscribtiondata>':["subscribtiondata","unimplemented"],
'<ManyToOneRel: client.calldata>':["calldata","unimplemented"],
'<ManyToOneRel: client.extracommunicationdata>':["extracommunicationdata","unimplemented"],
'client.Client.creation_date':["creation_date","Creation Date"],
'client.Client.first_name':["first_name","First Name"],
'client.Client.last_name':["last_name","Last Name"],
'client.Client.address':["address","Address"],
'client.Client.city':["city","City"],
'client.Client.phone_number':["phone_number","Main Phone"],
'client.Client.creator_of_user':["creator_of_user","Client Creator"],
'client.Client.status':["status","Status"],
'client.Client.status_change_time':["status_change_time","Last Time Status Changed"],
'client.Client.allocated':["allocated","Currently Allocated To Talpan"],
'client.Client.group':["group","Owner Group"],
'client.Client.tags':["tags","Tags"],
'client.Client.tagged_items':["tagged_items","Tagged Item"],
    }

    column_name = []
    column_field = []

    for field in data:
        field = str(field)
        if field in columns.keys():
            column_field.append(columns[field][0])
            column_name.append(columns[field][1])


    return zip(column_name, column_field)

# view function
def AllClientts(request):
    user_groups = []
    for groups in request.user.groups.all():
        if groups.name != MANGER_GROUP_NAME:
            user_groups.append(groups)

    # get all object that belongs to the requested user groups
    tableData =  Client.objects.filter(group__in=user_groups)

    return render(request, "client/clients.html", {"objects": tableData, "columns_name":HelperColumnesFieldsAndNames(Client._meta.get_fields()) })
def帮助列字段和名称(数据):
列={'':[“未执行支付”,“未执行”],
'':[“订阅数据”,“未实现”],
'':[“calldata”,“未实现”],
'':[“通信外数据”,“未实现”],
“client.client.creation_date”:[“creation_date”,“creation date”],
'client.client.first_name':[“first_name”,“first name”],
'client.client.last_name':[“last_name”,“last name”],
'client.client.address':[“address”,“address”],
'client.client.city':[“city”,“city”],
'客户机.客户机.电话号码':[“电话号码”,“主电话”],
'client.client.creator\u of_user':[“creator\u of_user”,“client creator”],
'client.client.status':[“status”,“status”],
'client.client.status_change_time':[“status_change_time”,“Last time status Changed”],
'client.client.allocated':[“已分配”,“当前已分配给Talpan”],
'client.client.group':[“组”,“所有者组”],
'client.client.tags':[“tags”,“tags”],
'client.client.tagged_items':[“tagged_items”,“tagged Item”],
}
列名称=[]
列_字段=[]
对于数据中的字段:
字段=str(字段)
如果字段位于columns.keys()中:
列\字段。追加(列[字段][0])
列名称。追加(列[字段][1])
返回zip(列名称,列字段)
#视图功能
def所有客户端(请求):
用户组=[]
对于request.user.groups.all()中的组:
如果是groups.name!=经理组名称:
用户\组。附加(组)
#获取属于请求的用户组的所有对象
tableData=Client.objects.filter(group\u in=user\u groups)
返回render(请求,“client/clients.html”,{“objects”:表格数据,“columns\u name”:helperColumnsFieldsAndNames(client.\u meta.get\u fields())})
脚本部分中的非工作示例

{% block content %}
<div class="d-flex  pt-5 pb-2">

<!-- Need to be set to colums initials names using django -->
<div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Show
    </button>

    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton" id="swichable_column">
        <!--reate fileds name and Showable names -->
        {% for name in columns_name %}
            <div class="checkbox">
                <label>
                    <input type="checkbox" checked="checked" class="chackable_name" value="{{ name.1 }}">{{ name.0 }}</label>
            </div>
        {% endfor %}
    </div>
</div>
</div>


<div id="example-table"></div>


{% endblock content %}
{%block content%}
显示
{列中的名称为%u name%}
{{name.0}}
{%endfor%}
{%endblock内容%}
在我的tamplet中的脚本部分-不工作,甚至执行相同的代码

<script>

    //define some sample data
 var tabledata = [
    {id:1, first_name:"Oli Bob", age:"12", col:"red", dob:""},
    {id:2, name:"Mary May", age:"1", col:"blue", dob:"14/05/1982"},
    {id:3, name:"Christine Lobowski the gratest and latest", age:"42", col:"green", dob:"22/05/1982"},
    {id:4, name:"Brendon Philips", age:"125", col:"orange", dob:"01/08/1980"},
    {id:5, name:"Margret Marmajuke", age:"16", col:"yellow", dob:"31/01/1999"},
 ];

 $(function() {
    var table = new Tabulator("#example-table", {
    height:205, // set height of table (in CSS or here), this enables the Virtual DOM and improves render speed dramatically (can be any valid css height value)
    data:tabledata, //assign data to table
    layout:"fitColumns",
    pagination:"local",
    paginationSize:6,
    paginationSizeSelector:[3, 6, 8, 10],
    columns:[ //Define Table Columns

        {% for name in columns_name %}
            {title:"{{ name.0 }}", field:"{{ name.1 }}", align:"center", cellClick:function(e, cell){alert("cell clicked - " + cell.getValue())}},
        {% endfor %}

    ]});




</script>

//定义一些示例数据
var表格数据=[
{id:1,名字:“奥利·鲍勃”,年龄:“12”,上校:“红色”,出生日期:},
{id:2,姓名:“玛丽·梅”,年龄:“1”,上校:“蓝色”,出生日期:“14/05/1982”},
{id:3,姓名:“克里斯汀·洛博夫斯基最伟大、最新”,年龄:“42”,上校:“格林”,出生日期:“22/05/1982”},
{id:4,姓名:“Brendon Philips”,年龄:“125”,上校:“橙色”,出生日期:“1980年8月1日”},
{id:5,姓名:“Margret Marmajuke”,年龄:“16岁”,上校:“黄色”,出生日期:“31/01/1999”},
];
$(函数(){
var table=新制表器(“示例表”{
height:205,//设置表的高度(在CSS中或此处),这将启用虚拟DOM并显著提高渲染速度(可以是任何有效的CSS高度值)
data:tabledata,//将数据分配给表
布局:“fitColumns”,
分页:“本地”,
分页大小:6,
分页大小选择器:[3,6,8,10],
列:[//定义表列
{列中的名称为%u name%}
{title:{{name.0}},字段:{{name.1},对齐:“中心”,单元格单击:函数(e,单元格){alert(“单元格单击-”+cell.getValue())},
{%endfor%}
]});

显然,如果我直接返回zip对象,它是可存储的,并且只能使用一次


我将
zip()
替换为
list(zip())
,解决了这个问题

显然,如果我直接返回zip对象,它是可存储的,只能使用一次


我将
zip()
替换为
list(zip())
,解决了这个问题

您是否在浏览器中检查了源文件?您在脚本标记中看到了什么?是“列”只是空的?在源代码中我有列只是空的我认为它是空的,因为我在脚本之前第一次使用对象?我如何重用对象?你在浏览器中检查了源文件了吗?你在脚本标记中看到了什么?是“列”只是空的?在源代码中我有列只是空的我认为它是空的,因为我在脚本之前第一次使用对象?我如何重用对象?