This is my models.py file
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
from django.db import models
from django.utils import timezone
import datetime
class Jugadores(models.Model):
nombre = models.CharField(max_length=30, default='')
edad = models.IntegerField()
posicion = models.CharField(max_length=15, default='')
def __str__(self):
return self.nombre
class Equipo(models.Model):
pos = models.IntegerField(default=1)
nombre = models.CharField(max_length=20, default='')
puntos = models.IntegerField(default=0)
jugadores = models.ManyToManyField(Jugadores)
def __str__(self):
return self.nombre
My doubt is that I want to show in my template called players.html, the list of players linked to each team. That is, if my team is called Barcelona, when clicking on the link to Barcelona, I will see the list of Barcelona players. And I see the list of all players entered in my database.
This is my views.py file
from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader ,context
from .models import Equipo, Jugadores
def inicio(request):
return render(request, 'index.html')
def ver_equipo(request):
equipo = Equipo.objects.all()
context = {'equipos': equipo}
return render(request, 'equipos.html', context)
def ver_clasificacion(request):
clasificacion = Equipo.objects.order_by('puntos')
context = {'clasificaciones': clasificacion}
return render(request, 'clasificacion.html', context)
def ver_jugadores(request):
jugador = Jugadores.objects.all()
context = {'jugadores': jugador }
return render(request, 'jugadores.html', context)