How can I use a variable as index in django template?
Right now I get this error:
Exception Type:
TemplateSyntaxError
Exception Value:
Could not parse the remainder: '[year]' from 'bikesProfit.[year]'
I also tried {{ bikesProfit.year }}
but this gives an empty result.
{% for year in years_list %}
<tr>
<th>Total profit {{ year }}:</th>
</tr>
<tr>
<th></th>
<th> {{ bikesProfit[year] }} </th>
...
It's possible, but goes against the grain of the Django model...
If you have items of interest, then ideally your view should return an object just containing those items, (OTTOMH something like):
It's a very common question, there are a lot of answers on SO.
You can make custom template filter:
Usage:
I don't think this is directly possible (edit: you can manually implement it with filters, as shown in goliney's answer). The documentation says:
If your case is not more complicated than what you're showing, the best solution in my opinion would be to loop over
bikeProfit
instead.