Reputation: 445
I'm trying to display a multidimensional array in a template through Django but I can't seem to loop through each row/col. I manage to print it out but it comes out like this which has the brackets because I'm just printing out the object:
But I need it to come out like this:
This is my class:
class VigenereCipher:
squareAlphabet = []
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def __init__(self):
self.squaredAlphabet=[ [ 'A' for x in range(26)] for j in range(26)]
#print(self.squaredAlphabet)
for x in range(len(self.letters)):
# self.squaredAlphabet[x][x] = self.letters[x]
# print(self.squaredAlphabet[x][x])
#print(x)
j = 0
for y in range(len(self.letters)):
#print(x)
j = y + x
#print(j)
if(j > 5):
j = j - 26
#print(j)
# print(self.letters[j])
self.squaredAlphabet[x][y] = self.letters[j]
#print(self.squaredAlphabet[x][j])
#print(self.squaredAlphabet)
# if ((y + x) > 5):
# y = y - 5
# #print(self.letters[y])
# self.squaredAlphabet[x][y] = self.letters[y]
for x in range(len(self.squaredAlphabet)):
for y in range(len(self.squaredAlphabet)):
print("Position: x", x, "y", y, "=", self.squaredAlphabet[x][y])
def getSquaredAlphabet(self):
return self.squaredAlphabet
Views.py:
def vigenereHome(request):
object = VigenereCipher()
x = object.getSquaredAlphabet()
return render(request, "VigenereCipher.html", {'x' : x})
And the template is:
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<meta charset=utf-8>
<link rel="stylesheet" href="{% static "CryptoWeb/css/VigenereCSS.css" %}"/>
<link rel="stylesheet" href="{% static "CryptoWeb/css/bootstrap.css" %}"/>
<link href='https://fonts.googleapis.com/css?family=Orbitron' rel='stylesheet' type='text/css'/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="{% static "CryptoWeb/js/VigenereJavascript.js" %}"></script>
<script type="text/javascript" src="{% static "CryptoWeb/js/bootstrap.min.js" %}"></script>
<title>Vigenere Cipher</title>
</head>
<body>
<table border="1">
{% for i in x %}
<tr>
<td>
<div <div align='center'><font face='arial' size='1'>
{# {% load VigenereTags %}#}
{# {{i|index:0}}#}
{{i}}
</font>
</div>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
EDIT:
I know that I can print it out using {{i.0}}
and so on but there must be a way to loop through i
without having to write it down literally.
Upvotes: 0
Views: 1051
Reputation: 3666
that's because each i
is a row and it requires an additional loop. Here's a small example: arr = [['a', 'b', 'c'], ['d', 'e', 'f']]
and I want to print every element separately a b c d e f
:
for x in arr:
for y in x:
print y
Upvotes: 3