首页 > 解决方案 > 将 python 列表从上下文传递给 JS 函数

问题描述

我有一个视图,可以为每个国家/地区呈现数据字典,如下所示:

def InvoicingDashboardView(request):
    # For reference - "c" is the context dictionary, and it's already initialized by this point

    for client in Client.objects.all():
        fte = frame.fte(c['start_date'], c['end_date'], time = 'external', client = client.id)

        if fte > 0:
            c['countrys'][client] = {
                'name': client.name,
                'fte': fte,
                'var_fte': fte - frame.fte(c['var_start_date'], c['var_end_date'], client = client.id),
                'daily_ftes': [frame.fte(day, day) for day in c['label_days']],
            }
    

    return render(request, 'dashboards/invoicing.html', context = c)

正如你所看到的,例如,如果我打电话{{countrys.client.fte}}给那个客户,我会得到 fte。

到目前为止一切都很好,但我需要daily_ftes一个 JS 函数,如下所示:

<div class="dash_countrys_ftes">
    {% for country, items in countrys.items %}
        <div class="dash_country_block">
        {% with name=items.name fte=items.fte variation=items.var_fte flag=country.flag.url %}
            {% include 'dashboards/charts/country_block.html' %}
        {% endwith %}
        </div>
        <script>
            countryFteVariation("{{items.daily_ftes|escapejs}}") // HERE
        </script>
    {% endfor %}
</div>

问题是这daily_ftes是一个python列表。

为了让 javascript 将其读取为列表而不是字符串,我可以在这里做什么?

标签: pythondjango

解决方案


推荐阅读