This is a simple code created only to show the probem, obviusly the task that this simple code is trying to do can be done without Javascript, but I need to know how I can do this using Javascript, because In my real code I need it, and It cant't be done without Javascript (basically I create a dinamic 2D array with information from the view)
in view.py
in def play_game function
...
players = Player.objects.all()
context = {
'players' : players
}
return render(request,'play_game.html', context)
in model.py
class Player(models.Model):
game = models.ForeignKey(Game, on_delete=models.CASCADE, blank=True, null=True)
loose = models.IntegerField( default=0)
win = models.IntegerField(default=0)
name = models.CharField(max_length=9)
In play_game.html
<script>
var players = "{{players}}";
for (player in players) {
document.write(player.name);
document.write(" Win:"+ player.win);
document.write("Loose: " + player.loose);
};
</script>