Error while trying to create a school timetable, in Django, that is automatically filled with buttons on the correct time and week day

I am trying to create a Django school timetable that is automatically filled with buttons once its created. I spent a lot of time trying to come up with a way since im still not very experience in programming and very new to Django, but the way I tried to do it was with "for" loops, while using a nested dictionary on the views, but due to my lack of understanding and experience i cannot figure out where the error (VariableDoesNotExist at /timetable/ Failed lookup for key [end_time] in 'monday') is comming from.

this is my models.py of the schedule table(some stuff is in portuguese):


class Horario(models.Model):
    ID_Horario = models.BigAutoField(primary_key=True)
    ID_Sala = models.ForeignKey(Sala, on_delete=models.CASCADE)
    ID_Turma = models.ForeignKey(Turma, on_delete=models.CASCADE)
    ID_Professor = models.ForeignKey(Professor, on_delete=models.CASCADE)
    Hora_inicio = models.TimeField()
    Hora_fim = models.TimeField()
    Dia_semana = models.CharField(max_length=100)
    Disciplina = models.CharField(max_length=100)

this is my views page(where i would guess I did something wrong):

def timetable(request):
    horario_entries = Horario.objects.all()
    timetable_data = {}

    for entry in horario_entries:
        day_of_week = entry.Dia_semana
        subject = entry.Disciplina
        start_time = entry.Hora_inicio.strftime('%H:%M')
        end_time = entry.Hora_fim.strftime('%H:%M')
        

        
        timetable_data[day_of_week] = {}

        
        timetable_data[day_of_week][start_time] = {}

        
        timetable_data[day_of_week][start_time][end_time] = {}

        timetable_data[day_of_week][start_time][end_time][subject] = {'entries': []}


        # Append the entry to the 'entries' list
        timetable_data[day_of_week][start_time][end_time][subject]['entries'].append(entry)

    return render(request, 'testetimetable.html', {'timetable_data': timetable_data})

and finally this is my error log:

Error during template rendering
In template C:\Users\danie\OneDrive\Documentos\PAP\teste16\django-soft-ui-dashboard-master\home\templates\testetimetable.html, error at line 26

Failed lookup for key [%s] in %r
16      <th>monday</th>
17      <th>tuesday</th>
18      <th>wednesday</th>
19      <th>thursday</th>
20      <th>friday</th>
21      <th>saturday</th>
22      <th>sunday</th>
23    </tr>
24    {% for x, schedule in timetable_data.items %}
25    <tr>
26      <th>{{ x.start_time|add:" - "|add:x.end_time }}</th>
27  
28      {% for day, entries in schedule.items %}
29        <td>
30          {% for entry in entries %}
31            <!-- Here you can create buttons or display other information -->
32            <button>{{ entry.subject }}</button>
33          {% endfor %}
34        </td>
35      {% endfor %}
36    </tr>

I tried changning most stuff to english for easier understanding.

Im not sure if there is an easier way to do what i intend, so if there is i would love to know

Back to Top