【问题标题】:removing [u''] in the django template删除 django 模板中的 [u'']
【发布时间】:2016-04-14 22:52:00
【问题描述】:


我正在尝试将 JSON 加载到 Django 模板中,这很顺利。但我得到 [] 围绕它和 'u 在它之前。我想知道是否有什么我可以做的。这是我使用的代码:

json_response = requests.get(endpoint + "appointments?user="+user+"&access_token="+token+"&start="+str(int(timestamp_start))+"&end="+str(int(timestamp_end))+"&valid=true").json()
appointmentsVrijdag = json_response['response']['data']
return render(request, template, {"appointmentsVrijdag": appointmentsVrijdag})

然后,要在模板中加载它,我使用以下代码:

            {% for appointment in appointmentsVrijdag %}
            {% if appointment.cancelled != True %}
                <tr>
                     <td>
                         {{ appointment.teachers }}
                         {{ appointment.subjects }}
                     </td>
                </tr>
            {% endif %}
        {% endfor %}

我希望得到一些帮助!

【问题讨论】:

    标签: python html json django django-templates


    【解决方案1】:

    假设appointment.teachersappointment.subjects 是列表,您可以使用built-in join filter 加入模板中的项目:

    {{ appointment.teachers|join:", " }}
    {{ appointment.subjects|join:", " }}
    

    【讨论】:

      猜你喜欢
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 2021-07-18
      • 1970-01-01
      • 2017-03-01
      • 2011-05-30
      • 2021-07-20
      相关资源
      最近更新 更多