I'm using django 1.11 and python 3.5.2.
This is the important part of my code in views.py
ListaValoresLeidosXML = []
for sec in secciones:
print(sec.contenidoxml)
treexml = ET(fromstring(sec.contenidoxml))
Lista = []
for child in treexml.findall('caracteristica'):
nombre = child.find('nombre').text
Lista.append(nombre)
ListaValoresLeidosXML.append(Lista)
for lista in ListaValoresLeidosXML:
for valor in lista:
print(valor)
print('*****')
return render(request, 'editarCatElem.html', {
'elemento': ElemSeg,
'link': tutorial,
'subelementos': subelementos,
'secciones': secciones,
'listaXML' : ListaValoresLeidosXML
})
I'm reading a string field in my database, it contents a xml-formatted text, using treexml I can get all the attributes.
Finally I have a list of lists, each list have values of a section, it means if I have n sections then I'll have n lists inside a list.
I'm sending a list called ListaXML to my template.
In my template I'm trying to iterate it
{% for seccion in secciones %}
<div style="display: block" class="form-group">
<div style="display: block" class="form-group">
<label class="control-label col-md-2" for="nombreSeccion">Sección <span class="required">*</span></label>
<div class="col-md-2"><input type="text" value='{{ seccion.nombre }}' id="nombreSeccion" name="secciones[][nombreSeccion]" class="form-control col-md-2"></div>
<label class="control-label col-md-2" for="descSeccion">Descripción <span class="required">*</span></label>
<div class="col-md-2"><input type="text" value='{{ seccion.descripcion }}' id="descSeccion" name="secciones[][descripcionSeccion]" class="form-control col-md-2"></div>
</div>
<div class="col-md-offset-2 col-md-7">
<div class="form-horizontal form-label-left" style="background:lightblue; padding:8px;">
{% for caracteristica in listaXML %} # <-- Here is the problem!!
<div class="form-group">
<label class="control-label col-md-2" for="nombreCampo">Campo <span class="required">*</span></label>
<div class="col-md-4">
<input type="text" value='{{ caracteristica }}' name="secciones[][campos][][nombreCampo]" id="nombreCampo" class="form-control col-md-2">
</div>
</div>
{% endfor %}
<div class="form-group">
<button type="button" class="btn btn-primary col-sm-offset-0 col-sm-3" onclick="agregarCampo(event);"><i class="fa fa-plus"> </i> Agregar campo</button>
<button type="button" class="btn btn-danger col-sm-offset-1 col-sm-3" onclick="eliminarCampo(event);"><i class="fa fa-minus"> </i> Eliminar campo</button>
</div>
</div>
</div>
</div>
{% endfor %}
The problem is I need to do this with every repetition:
{% for caracteristica in listaXML.0 %}
{% for caracteristica in listaXML.1 %}
{% for caracteristica in listaXML.2 %}
{% for caracteristica in listaXML.3 %}
...
I need to increment the final number (the index of the list).
I've tried
{% for caracteristica in listaXML.forloop.counter0 %}
or
{% for caracteristica in listaXML.{{forloop.counter0}} %}
but it doesn't work.
Any idea on how the code works?