0

With Django 1.8 template, tried to POST table data. I set <TABLE> id = 'tasktable'. But in request information, there's no data of 'tasktable'

Also, how can I access dynamic table POST data in Django views? Is only ways to make id each row?
(like <td><input id='1_data'></td><td><input id='2_data'></td>

POST request info

Below Code is template.

{% extends "base.html" %}

{% load staticfiles %}

{% block scripts %}
<script src="//code.jquery.com/jquery.min.js"></script>
<script>
$(function () {
    $('#btn-add-row').click( function() {
        $('#tasktable > tbody:last').append('<tr><td><input type="text"></td><td><select name="format"><option value="INT">INT</option><option value="TEXT">TEXT</option></select></td></tr>');
    });
    $('#btn-delete-row').click( function() {
        $('#tasktable > tbody:last > tr:last').remove();
    });
});
</script>
{% endblock %}

{% block title %}
Angel Feeder - Manager
{% endblock %}

{% block body %}
<div class="container">
    <form class="" method="post" action="">
        {% csrf_token %}
        {{ taskform }}
        <br>
        <button id="btn-add-row" type="button">+</button><button id="btn-delete-row" type="button">-</button>
        <table name='tasktable' id='tasktable' class="table">
            <thead>
                <tr>
                    <th>Data name</th>
                    <th>Format</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><input type="text"></td>
                    <td><select name="format"><option value="INT">INT</option><option value="TEXT">TEXT</option></select></td>
                </tr>
            </tbody>
        </table>

        <button class="btn btn-lg btn-primary" type="submit">set task</button>
    </form>
</div>
{% endblock %}
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
davinno
  • 15
  • 7

1 Answers1

0

Only form elements with a name attribute will have their values passed when submitting a form.

name is not an allowed attribute of <table> in HTML5 https://stackoverflow.com/a/13677670/3033586

Obviously you have to set unique names for each form element inside table>tr>td so you can reference it after post

If you want simple styled [model]forms (for example with bootstrap) - look at http://django-crispy-forms.readthedocs.org/en/latest/

Community
  • 1
  • 1
madzohan
  • 11,488
  • 9
  • 40
  • 67