D
D
Denis Pupchenko2017-03-06 10:35:21
Django
Denis Pupchenko, 2017-03-06 10:35:21

How to understand which object the input belongs to?

After each section, I added an input in which the user indicates the required number of questions related to a particular section, in order to further pull the required number of questions from the database and display it.
It looks like this
93531b443d77486d916db22d8cd85831.png
How do I determine which section (object from the database) the input belongs to (chekbox or text)?
Here is the template code

<form action="/vibor_test/gener/" method ="post">
     {% csrf_token %}
    <ol>
        {% recursetree nodes.get_descendants %}
            <li>
                {{ node.name }}
                {% if node.level == 1 %}
                    <input type="text" name="text" value="" >
                {% endif %}
                {% if node.level == 2 %}
                    <input type="checkbox" name=""  value="">
                {% endif %}
                {% if not node.is_leaf_node %}
                    <ol class="children">
                        {{ children}}
                    </ol>
                {% endif %}
            </li>
        {% endrecursetree %}
    </ol>
     <input class="button"  type="submit" value="Пройти тест">
 </form>

In template: :
<input type="text" name="text_{{ node.id }}" >
views.py
textbox_list = [x for x in request.POST if x.startswith('text_')]
 args['textbox'] = textbox_list[0].replace('text_', '')

When displayed on a page.
{% for idin  textbox %}
   {{ id}}
{% endfor %}

If the id is two-digit or more, then it displays a separate digit on each line (at each iteration).

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question