我有以下模板:
<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>
以下观点:
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
)时,除了表头之外什么都不输出。它看起来像这样:
我无法弄清楚为什么Django没有把输出放到单元格中。我希望列表中的元素None
没有输出,然后只需要所有其他元素的元素内容(都是字符串)。有什么想法吗?
答案 0 :(得分:3)
你正在重复错误的事情。您的日历是列表清单;你应该遍历每一行,然后迭代该行中的每一列。 import numpy as np
Q = [[0 for j in range(2)] for i in range (313)] # Creates an array of zeros with 2 columns and 313 rows
for j in range (1, 3):
xN = 0.5 + 0.5*random.uniform(0, 1)
r = 3.8 + 0.15*random.uniform(0, 1)
for i in range (1, 313+1):
xNew = logistic_map(xN, r)
xN = xNew
Q[i][j] = xNew
Q = np.array(Q) # Convert this to an array and take the transpose of it
完全无关紧要。
week_length
答案 1 :(得分:0)
您没有正确访问row
对象中的项目。它们位于索引i
,而不是名为i
的属性。
您可以执行以下操作:
{% for i in row %}
<td>{{ i }}</td>
{% endfor %}
因为row
是您要访问的元素列表。这样可以避免传递week_length
变量。