【发布时间】:2020-05-19 14:57:02
【问题描述】:
我想在给定的板上显示数组元素。但是我做不到。
这是我的views.py:
from django.shortcuts import render
from django.http import HttpResponse
import numpy as np
import random
from time import sleep
#the home will accept request when the client will send it
def play(request):
#for request a response is created
#in HTTP format(changed)
return render(request,'play.html',{'name':'M'})
# Creates an empty board
def create_board(request):
arr=np.array([[0, 0, 0], [0, 0, 0],
[0, 0, 0]])
return render(request,'play.html',{'array':arr})
以下是 HTML 代码:
<table>
<tr>
<td></td>
<td class="vert"></td>
<td></td>
</tr>
<tr>
<td class="hori">{{array}}</td>
<td class="vert hori"></td>
<td class="hori"></td>
</tr>
<tr>
<td></td>
<td class="vert"></td>
<td></td>
</tr>
</table>
下面是我希望显示数组元素的网页表格:
编辑 1: 这就是我尝试创建表的方式:
<table>
{% for row in array %}
<tr>
{% if forloop.first or forloop.parentloop.first%}
<td class="hori"></td>
{% else %}
<td></td>
{% endif %}
{% for col in row %}
{% if forloop.first or forloop.parentloop.first %}
<td class="vert"></td>
{% else %}
<td></td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
【问题讨论】:
-
还要注意在 Jinja 中变量名前后需要一个空格。所以应该是 {{ array }} 而不是 {{array}}
-
不,他们没有解决问题
-
你试过用两个循环解决它吗?在这两个 for 循环中,您还应该添加行
标签: python django numpy django-views django-templates