如何在django模板中显示python列表

时间:2022-01-05 20:22:47

I am new to python and django. I want to know how can I dispaly python list in django template. The list is a list of days in weeks e.g day_list = ['sunday','monday','tuesday']

我是python和django的新手。我想知道如何在django模板中显示python列表。该列表是几周内的天数列表,例如day_list = ['sunday','monday','tuesday']

2 个解决方案

#1


12  

Pass it to the template as a context and then simply iterate over it.

将其作为上下文传递给模板,然后简单地迭代它。

{% for day in day_list %}
    {{ day }}
{% endfor %}

This is the documentation for the for tag. I recommend you go through the Django tutorial, and in part 3 they go over passing stuff to your templates, including iterating over sequences of stuff (like lists).

这是for标签的文档。我建议你阅读Django教程,在第3部分中,他们将过去的东西传递给你的模板,包括迭代一些东西(比如列表)。

#2


1  

You need to pass it in the template context.

您需要在模板上下文中传递它。

render_to_response(..., {"day_list": ['sunday','monday','tuesday']} )

#1


12  

Pass it to the template as a context and then simply iterate over it.

将其作为上下文传递给模板,然后简单地迭代它。

{% for day in day_list %}
    {{ day }}
{% endfor %}

This is the documentation for the for tag. I recommend you go through the Django tutorial, and in part 3 they go over passing stuff to your templates, including iterating over sequences of stuff (like lists).

这是for标签的文档。我建议你阅读Django教程,在第3部分中,他们将过去的东西传递给你的模板,包括迭代一些东西(比如列表)。

#2


1  

You need to pass it in the template context.

您需要在模板上下文中传递它。

render_to_response(..., {"day_list": ['sunday','monday','tuesday']} )