Answer the question
In order to leave comments, you need to log in
How to get specific value from dictionary in django template?
Hello.
A dictionary of the type is passed to the template:
{'1A': ['Name', 'State', 'Name2', 'State2']}
{'2A': ['Name', 'State', 'Name2', 'State2' , 'Name3', 'State3', ...]}
There is a table in the template that needs to be filled like this:
<tr>
<input type="hidden" name="stud" value="{{ Name }}" />
<td>{{ Name }}</td>
<input type="hidden" name="state" value="" class="state"/>
<td id="editable">{{ State }}</td>
</tr>
{% for school_class, data in students_data.items %}
<div id='{{ school_class }}' class="tab-pane fade">
<form method="post" action="" class="data">
{% csrf_token %}
<table class="table sch_{{ school_class }}">
<thead>
<input type="hidden" name="school" value="{{ school_class }}" />
<th colspan="2">{{ school_class }}</th>
</thead>
<tbody>
{% for student in data %}
<tr>
<input type="hidden" name="stud" value="{{ student }}" />
<td>{{ student }}</td>
<input type="hidden" name="state" value="" class="state"/>
<td id="editable">{{ student }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="col-md-12" style="text-align: right;">
<button type="submit" class="btn btn-success" id="send">Сохранить</button>
</div>
</form>
</div>
{% endfor %}
files = DataFile.objects.values_list('file')
for file in files:
date, data = parse_table(media_root + '/' + file[0])
if date == time.strftime('%d.%m.%Y'):
full_dict.update(data.items())
context['date'] = date
context['class_names'] = full_dict.keys()
context['students_data'] = full_dict
Answer the question
In order to leave comments, you need to log in
To find out how to pull out the values correctly, you must first find out why they generally come in such an inconvenient form, and if there is no way to find out and fix it, convert it to a more convenient form, for example, into nested lists:
def table_view(request):
data = {'2A':
['Name', 'State',
'Name2', 'State2',
'Name3', 'State3',
'Name4', 'State4',
'Name5', 'State5']
}
data['2A'] = [data['2A'][i:i+2] for i in range(0, len(data['2A']), 2)]
return render(request, 'app/template.html', {'students_data': data})
{% for student in data %}
<tr>
<input type="hidden" name="stud" value="{{ student.0 }}" />
<td>{{student.0 }}</td>
<input type="hidden" name="state" value="" class="state"/>
<td id="editable">{{ student.1 }}</td>
</tr>
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question