Show items from a list linked to manytomanyfield

0

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)
    
asked by Rafael Romero Medina 25.01.2017 в 22:09
source

1 answer

1

Well what is usually done is that given your list of Teams in your view of ver_equipo in the template what you do is go through all the teams, in that way, add a link to a new view, an example It would be like that.

<table>
{% for equipo in equipos %}
<tr>
    <th>{{ equipo|upper }}</th>
    <td><a href="{% url "ver_jugadores_equipo" equipo.id %}">Ver equipo</a></td>
</tr>
{% endfor %}
</table>

Then in your views.py you add a new view:

...
from django.shortcuts import get_object_or_404
def ver_jugadores_equipo(request, pk):
    equipo = get_object_or_404(Equipo, pk=pk)
    jugadores = equipo.jugadores.all()
    return render(request, 'lista_jugadores_por_equipo.html', {'jugadores': jugadores})
...

Then in your urls.py

...
# agregas dentro de tu arreglo de urlpatterns
url(r'jugadores/equipo/(?P<pk>\d+)/', views.ver_jugadores_equipo, name='ver_jugadores_equipo'),
...

And it only remains that you create the template of 'lista_jugadores_por_equipo.html' that comes with the players of the team that you clicked on the link. And you must go through and start to list

Any questions, comments.

    
answered by 26.01.2017 в 15:00