Django模板迭代通过结构

时间:2017-10-31 11:46:59

标签: python html django templates

早上好 我有来自后端的以下结构:

  List<int> clientIds = new List<int> { 1, 2, 3 };
  IQueryable<User> users = _context.Users
        .Include(x => x.Clients)
        .Where(x => x.Clients.Select(cl => cl.Id).Intersect(clientIds).Any());

我需要以复选框的形式将此结构放在我的模板中,如下所示:

sentences_info = [
    {
        "keyword": "",
        "sentences": [
            {
                "sentence_id": "",
                "sentence": ""
            },
            ...
        ],
        ...
    },
    ...
]

我尝试了一些迭代,但到目前为止将它放在模板上是没有意义的。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

你试过这个吗? 条件是sentences_info在上下文中。

<form>
{% for info in sentences_info %}
<h3>Keyword: {{ info.keyword }}</h3>
<div class="form-check">
{% for sentence in info.sentences %}
    <label class="form-check-label">
        <input type="checkbox" class="form-check-input" id="{{ sentence.sentence_id }}" value="{{ sentence.sentence_id }}">
        {{ sentence.sentence }}
    </label>
{% endfor %}
</div>
{% endfor %}
</form>

答案 1 :(得分:1)

你应该能够做到;

{% for sentence in sentence_info %}
    <h3>Keyword: {{ sentence.keyword }}</h3>
    {% for s in sentence.sentences %}
        <div class="form-check">
            <label class="form-check-label">
                <input type="checkbox" class="form-check-input" id="{{ s.sentence_id }}" value="{{ s.sentence_id }}">
            {{ s.sentence }}
        </label>
    {% endfor %}
{% endfor %}