0

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:

enter image description here

But I need it to come out like this:

enter image description here

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.

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.