Issue
This is my first question (sorry for any english mistakes), and I appreciate your help in advance! I'm working with a Django app (MVT). I have a view that returns a dictionary, and I would like to access individual elements of the dictionary in the template. However, in the template, I can render the complete dictionary without any issues, but I can't render individual elements. I'll show you the view that return the dict registros_por_dia.
class GrillaView(View):
template_name = 'grilla.html'
def get(self, request):
return render(request, self.template_name)
def contar_registros(self, fecha):
...
def post(self, request, fecha=None):
if fecha:
...
else:
# Esta parte se ejecutará cuando se envíe el formulario del mes y año
mes = int(request.POST.get('mes'))
ano = int(request.POST.get('ano'))
dias_en_mes = [dia for dia in range(1, monthrange(ano, mes)[1] + 1)]
registros_por_dia = {}
for dia in dias_en_mes:
fecha = datetime(ano, mes, dia).strftime('%Y-%m-%d')
contador = self.contar_registros(fecha)
registros_por_dia[dia] = contador
return render(request, self.template_name, {'dias_en_mes': dias_en_mes, 'registros_por_dia': registros_por_dia})
And now I'll show you the relevant part of the template.
...
{% if dias_en_mes %}
{% with fecha_format='%Y-%m-%d' %}
<div style="display: flex; flex-wrap: wrap;">
{% for dia in dias_en_mes %}
{% with fecha=dia|date:fecha_format %}
<div style="width: 50px; height: 50px; border: 1px solid black; margin: 5px;">
<span>{{ dia }}</span>
<br>
<span>{{registros_por_dia.dia}}</span>
</div>
{% endwith %}
{% endfor %}
</div>
{% endwith %}
{% endif %}
{% endblock %}
The line {{registros_por_dia.dia}} doesn't render any value. However, if I change it to {{registros_por_dia}}, it does render the entire dictionary. I've tried various variants without success.
<span>{{registros_por_dia|get:dia}}</span>
<span>{{registros_por_dia.get(dia, 'Valor predeterminado')}}</span>
{% with registro=registros_por_dia.dia %}<span>{{ registro }}</span> {% endwith %}
<span>{{registros_por_dia|getattr:dia}}</span>
<span>{{registros_por_dia[dia]}}</span>
<span>{{registros_por_dia|dictlookup:dia}}</span>
None of them works, does anyone know what I can do? Thank you very much!
I've tried various variants without success.
<span>{{registros_por_dia|get:dia}}</span>
<span>{{registros_por_dia.get(dia, 'Valor predeterminado')}}</span>
{% with registro=registros_por_dia.dia %}<span>{{ registro }}</span> {% endwith %}
<span>{{registros_por_dia|getattr:dia}}</span>
<span>{{registros_por_dia[dia]}}</span>
<span>{{registros_por_dia|dictlookup:dia}}</span>
None of them works, does anyone know what I can do? Thank you very much!
Solution
You may be looking for the dictionary's items
method:
{% for dia, contador in registros_por_dia.items %}
{{dia}}: {{contador}}<br />
{% endfor %}
Then of course if contador
is a datatype like a list, treat that like follows within the outer loop:
{% for dia, contador in registros_por_dia.items %}
{{dia}}: {% for c in contador %} {{c}} {% endfor %}<br />
{% endfor %}
Answered By - v25
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.