Good morning I have my application in Django 1.10 and Phyton 3, in which I will handle cards with an expiration date for each of the students. These cards have an expiration date [1], once the expiration date is over, they will go into inactive status. So they can reach a point where several students have several associated cards but only one active the rest inactive.
What I want to do is show in a template the students (who will be shown filtered by grade) and if the student has an active card that shows the data corresponding to the card, otherwise show these fields blank and a link to assign the card.
Views.py
def generaCarnet(request):
if request.method == 'POST':
query = request.POST['query']
student = Estudiante.objects.get( numeroDocumento = query )
grado = Estudiante.objects.filter( grado = student.grado )
return render(request,'generaCarnet.html',{'grupo':grado,})
else:
return render(request,'generaCarnet.html')
template
{% if grupo %}
<table class="table table-bordered alt">
<thead>
<tr>
<th class="active col-sm-1">Documento</td>
<th class="active col-sm-2">Nombres y apellidos </td>
<th class="active col-sm-2">Carnet </td>
<th class="active col-sm-2">Fecha Expedición </td>
<th class="active col-sm-2">Fecha Vencimiento </td>
<th class="active col-sm-1">Acción</td>
</tr>
</thead>
<tbody>
{% for ben in grupo %}
<tr>
<td>{{ ben.numeroDocumento }}</td>
<td>{{ ben.nombreUno|title }} {{ ben.nombreDos|title }} {{ ben.apellidoUno|title }} {{ ben.apellidoDos|title }}</td>
{% for carnet in ben.carnet_set.all %}
<td>{{ carnet.codigo }}</td>
<td>{{ carnet.fechaExpide }}</td>
<td>{{ carnet.fechaVence }}</td>
<td><a href="#imprimir" target="_blank"><i class="fa fa-print" title="Imprimir" aria-hidden="true"></i></a></td>
{% empty %}
<td></td>
<td></td>
<td></td>
<td><a href="#activar"><i class="fa fa-check" title="Activar" aria-hidden="true"></i></a></td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
models.py
class Estudiante(models.Model):
numeroDocumento = models.BigIntegerField()
apellidoUno = models.CharField(max_length=50)
apellidoDos = models.CharField(max_length=50)
nombreUno = models.CharField(max_length=50)
nombreDos = models.CharField(max_length=50)
grado = models.IntegerField() #Campo en comun entre varios estudiantes
class Carnet(models.Model):
codigo = models.IntegerField()
estudiante = models.ForeignKey(Estudiante)
fechaExpide = models.DateField(null=True)
fechaVence = models.DateField(null=True)
estado = models.NullBooleanField(null=True) # Activo o Inactivo del carnet
With what I have so far, it shows me all the cards that exist per student and Only I want the Active card to be shown per student.
[1] I would also appreciate if someone can tell me how to automate when the expiration date arrives, pass from active to inactive the Card.
I appreciate your cooperation