我正在尝试在Django中显示Vigenere Square但是我想为特定的行和列指定颜色,以便可以从方块的其余部分识别它。通过这种实现,整个方块都是彩色的。我希望第一行着色,第一列也着色。其余的必须是白色的。
模板:
<div class="hidden-xs">
<table id="show-table">
<tbody>
{% for row in vigenereExample %}
{% if row.0 %}
<tr style="background-color: #98AFC7;">
{% for col in row %}
<td>
{{col}}
</td>
{% endfor %}
</tr>
{% else %}
<tr>
{% for col in row %}
<td>
{{col}}
</td>
{% endfor %}
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
View.py
def vigenereHome(request):
object = VigenereCipher()
vigenereSquare = object.getSquaredAlphabet()
vigenereExample = object.geSquareExample()
context = {
'vigenereSquare' : vigenereSquare,
'vigenereExample' : vigenereExample
}
return render(request, "VigenereCipher.html", context)
这是构建Square的类 VigenereCipher.py
class VigenereCipher:
squareAlphabet = []
squareExample = []
exampleKey = "NETWORK"
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
nonCapLetters = "abcdefghijklmnopqrstuvwxyz"
def __init__(self):
# Initialize a list of lists. 27x27 array because of the Grid
self.squaredAlphabet=[ [ '' for x in range(27)] for j in range(27)]
self.squareExample=[['' for x in range(26)] for j in range(len(self.exampleKey) + 1)]
# Create Grid on column
for y in range(len(self.letters)):
#self.squaredAlphabet[0][y+1] = "x"
self.squaredAlphabet[0][y+1] = self.nonCapLetters[y]
self.squareExample[0][y] = self.letters[y]
# Create Grid on row
for y in range(len(self.letters)):
#self.squaredAlphabet[y+1][0] = "y"
self.squaredAlphabet[y+1][0] = self.nonCapLetters[y]
for y in range(len(self.exampleKey)):
self.squareExample[y+1][0] = self.exampleKey[y]
# Fill Vigenere Square
for x in range(len(self.letters)):
j = 0
for y in range(len(self.letters)):
j = y + x
if(j > 25):
j = j - 26
self.squaredAlphabet[x+1][y+1] = self.letters[j]
# For debugging purposes, display the Square.
# for x in range(len(self.squaredAlphabet)):
# for y in range(len(self.squaredAlphabet)):
# print("Position: x", x, "y", y, "=", self.squaredAlphabet[x][y])
# Fills the exampleSquare
for x in range(len(self.exampleKey)):
j = 0
for y in range(len(self.letters)):
j = self.letters.index(self.exampleKey[x]) + y
if(j > 25):
j = j - 26
self.squareExample[x+1][y] = self.letters[j]
print(self.squareExample)
def getSquaredAlphabet(self):
return self.squaredAlphabet
def geSquareExample(self):
return self.squareExample
答案 0 :(得分:1)
可能你需要的是
<div class="hidden-xs">
<table id="show-table">
<tbody>
{% for row in vigenereExample %}
<tr>
{% for col in row %}
{% if forloop.counter0 == 0 or forloop.parentloop.counter0 == 0 %}
<td style="background-color: #98AFC7;">
{% else %}
<td>
{% endif %}
{{col}}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>