niloofar niloofar - 2 years ago 100
Python Question

django-python create Multidimensional Array in


class PUser(models.Model):
phone = models.TextField(blank=True, null=True)
email = models.TextField()
txt = models.TextField(blank=True, null=True)

I want to create Multidimensional Array for that.

For now this is what I have in the function in

def main(request):
users = []
for i in range (5):
for a in range(3):
users[i][a] = PUser.objects.all()
return render(request, 'main.html', {'users': users})

But I know its not correct, its not working.

How should I edit it?

And how the code in the main.html should be?

I was thinking about something like
{{ users[2][3] }}
for example. How the code should be? (I have read same questions but was not helpful for me)

Answer Source

Since PUser.objects.all() returns an array of PUser object, you just have to write :

def main(request):
    users = PUsers.objects.all()
    return render(request, 'main.html', {'users': users})

and in your template, iterate on users array :

{% for user in users %}
    {{ }}
    {{ }}
    {{ user.txt }}
{% endfor %}

If you wanna print a specific user, you can by specifying its index :

{{ users[3].phone }}
{{ users[3].email }}
{{ users[3].txt }}
# or
{{ }}
{{ }}
{{ users.3.txt }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download