niloofar niloofar - 10 months ago 37
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)


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 }}