Alvaro
Alvaro

Reputation: 12037

Getting post data from html table inside a form? Django

I have a dynamically populated html table that can be edited by an user (they can add rows delete them, etc -this is managed through javascript-). The problem is, even when the table is inside the form, the server doesn't get any post data from it. Here is the code for the template:

{% extends 'base.html' %}
{% block content %}
    <script>
        function add_row(){
            var table = document.getElementById("body");
            var row = table.insertRow(table.rows + 1);
            var cell = row.insertCell(0);
            var combo1 = document.createElement("select");
            var option;
            {% for opt in options_all %}
                option = document.createElement("option");
                option.setAttribute("value", "{{ opt.id }}");
                option.text="{{ opt.description }}";
                combo1.add(opcion);
            {% endfor %}
            cell.appendChild(combo1);
            }
    </script>
    <form action='.' method='post'>
        {% csrf_token %}
        {{ form.as_p }}
        <input type="button" value="New row" onclick="add_row()">
        <table id="table_id">
            <thead>
                <tr>
                    <th>
                        Options
                    </th>
                </tr>
            </thead>
            <tbody id="body">

            </tbody>
        </table>
        <input type='submit' value='Submit'>
    </form>
{% endblock %}

How can I get the data from the select elements?? Thanks!! Edit: I have checked the request element and request.post doesn't have the desired data

Upvotes: 0

Views: 2699

Answers (1)

Alvaro
Alvaro

Reputation: 12037

SOLVED. You have to specify a name for each select element inside the table. (Not the option elements)

Upvotes: 0

Related Questions