In models.py:
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 views.py:
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.phone }}
{{ user.email }}
{{ 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.phone }}
{{ users.3.email }}
{{ users.3.txt }}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.