V
V
Valery Sivolapenko2017-10-06 23:22:09
Django
Valery Sivolapenko, 2017-10-06 23:22:09

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>

Complete table:
{% 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 %}

What is the right way to extract the values? With such a cycle, at each iteration I get the filling of the table with the same values, respectively.
Thanks to everyone who responds)
UPD
Excel is parsed into a dictionary. The class, the students in the class and the presence of the student. The number of students may vary. Here is the view code, I understand what needs to be corrected here
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

1 answer(s)
J
javedimka, 2017-10-07
@coveraver

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})

And then in the template:
{% 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 %}

or as an option - use namedtuple, but I don't know what the speed will be there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question