0
You can access the values within a variable in Django templates?
For example, I have this model:
class Avaliacao(models.Model):
post = models.TextField()
data_inclusao = models.DataField()
in the field post is recorded data a Dict, this way:
{'joao': ['gostei', 8], 'carla': ['nao gostei', 1] }
Let’s say in my/html template, I want to popular a table, how could I access the keys and values of that string ? Imagine that the data has been rendered to the data variable:
{% for avaliacao in dados %}
{{ avaliacao.post }}
{% endfor %}
The way out would be:
<html>
<table>
<thead>
<tr>
<th>Nome</th>
<th>Avaliacao: </th>
<th>Nota: </th>
</tr>
</thead>
<tr>
<td>JOAO</td>
<td>GOSTEI</td>
<td>8</td>
<tr>
<tr>
<td>CARLA</td>
<td>NAO GOSTEI</td>
<td>1</td>
<tr>
</table>
</html>
A
TextField
text guard, nodict
... If you’re using Dict before saving in the bank, just deserialize and use it as Dict...– fernandosavio
@fernandosavio I know this, unfortunately the data is recorded the way I mentioned. What I would need was to pick up these strings. and form new variables. If in the template it is not possible el, you know how to create a function in the model, to call the function in the template ?
– Rodolfo Sousa
You do not specify what the serialization format is, but apparently it is JSON.. Just use module
json
python. Sopt already has enough content on this, take a look.– fernandosavio
good, I’ll look. Thank you.
– Rodolfo Sousa