gpt4 book ai didi

python - Django 模板不会将值输出到 HTML 表格单元格

转载 作者:行者123 更新时间:2023-11-30 22:30:06 24 4
gpt4 key购买 nike

我有以下模板:

 <table>
<tr>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
</tr>

{% for row in table %}
<tr>
{% for i in week_length %}
<td>{{row.i}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>

以及以下 View :

def calendar(request):
template_name = 'workoutcal/calendar.html'

today_date = timezone.now()
today_year = today_date.year
today_month = today_date.month
today_day = today_date.day

table = maketable(today_date) # Creates something like: [[None, 1, 2, 3, 4, 5, 6],[7, 8, ...],...,[28, 29, 30, 31, None, None, None]]

template = loader.get_template('workoutcal/calendar.html')

#Workout.objects.filter("Workouts lie between certain dates")
context = {
'workout_list': Workout.objects.filter(date__year = today_year, date__month = today_month, date__day = today_day),
'table': table, # The calendar
'week_length': range(7),
}
return HttpResponse(template.render(context, request))

当我访问页面(localhost:8000/workoutcal)时,除了表头之外没有任何输出。它看起来像这样:

The table

inspecting the object

我不明白为什么 Django 没有将我的输出放入单元格中。我不希望列表中 None 的元素输出,然后只输出所有其他元素的元素内容(全部都是字符串)。有什么想法吗?

最佳答案

你正在迭代错误的事情。你的日历是一个列表的列表;您应该迭代每一行,然后迭代该行中的每一列。 week_length 完全无关。

{% for week in table %}
<tr>
{% for day in week %}
<td>{{ week }}</td>
{% endfor %}
</tr>
{% endfor %}

关于python - Django 模板不会将值输出到 HTML 表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46164063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com