Hey, there! How are you doing?
I guess you’re facing an annoying (but quite easy to solve) problem… am I right? Well, no more talk, let’s go ahead!
I can bet that the problem is that you didn’t put the desired set of components in a context variable. But what am I talking about?
See, in your views.py
you need to have something like:
1. Creating the Context in views.py
@login_required
def notifica_modelo(request, id):
modelos = Modelo.objects.all
notificar_clientes_modelo(id)
context = {'modelos': modelos}
return render(request, "pages/envia_notificacoes.html", context)
In that piece of code, the method notifica_modelo
stores all the Modelo
objects in the variable context
, which is just a dictionary containing key-value pairs.
2. Accessing the Context in the Template
To improve your understanding, here is the partial template envia_notificacoes.html
:
{% load static %}
{% load bootstrap_icons %}
<div class="container-lista">
<h2>Enviar notificações</h2>
<br>
<table id="datatable">
<tr>
<th>Notificação</th>
<th> </th>
<th> </th>
</tr>
{% for modelo in modelos %}
<tr>
<td>{{ modelo.assunto }}</td>
<td>
<a href="{% url 'exibe_modelo' modelo.id %}">
{% bs_icon 'search' color='blue' %}
</a>
</td>
<td>
<a href="{% url 'notifica_modelo' modelo.id %}">
{% bs_icon 'send' color='blue' %}
</a>
</td>
</tr>
{% endfor %}
</table>
</div>
By iterating through the variable modelos
, the data is retrieved from the context
dictionary passed from the view, and everything works smoothly.
Final Thoughts
See? Told you it was easy! If you have any questions or want to dive deeper, just drop a comment or message. Happy coding!
— Igor Magalhães Oliveira
Leave a Reply