(tables) using with formset

11 views
Skip to first unread message

Mike RA9FTM

unread,
Jun 2, 2009, 4:12:29 PM6/2/09
to Django Apps
Hello.
I am new to django and python. I need to add some editable cells to
django-table.
I would like to show how I added formset to django-tables rows.
Please comment it out, is it good or not to do like this.


Hmmm.... And what if it will be ModelTables and ModelFormSet?


Here it is:


class TstTable(tables.Table):
id = tables.TextColumn()
test = tables.TextColumn()

class TstForm(forms.Form):
id = forms.DecimalField(required=False)

def TestTable(request):

TstFormSet = formset_factory(TstForm, extra=0)
formset = TstFormSet(initial=[
{'id': 22,},
{'id': 123,},
])

data = [{'test: 'Germany',},
{'test': 'France',}]

for row in data:
row['id']=formset.forms.pop(0)['id']

table = TstTable(data)

if request.method == 'POST':
formset=TstFormSet(request.POST)
if formset.is_valid():
pass
for row in data:
row['id']=formset.forms.pop(0)['id']


return render_to_response('/testapp/test.html',
{'table': table,
'formset':formset,
},
context_instance=RequestContext
(request) )


Here is template:

<form name="superform" action="" method="POST">
<input type="submit" value="Save">
{{ formset.management_form }}


<div>

<table>
<thead>
<tr>
{% for column in table.columns %}
{% if column.sortable %}
{% if column.is_ordered %}
{% if column.is_ordered_reverse %}
<th class="sorted ascending">
{%else%}
<th class="sorted descending">
{%endif%}
{%else%}
<th>
{%endif%}
<a href="?sort={{ column.name_toggled }}">
{{ column }} </a> </th>
{% else %} <th>
{{ column }}</th>

{% endif %}
{% endfor %}


</tr>
</thead>
<tbody>

{% for row in table.rows %}

<tr>

<td> {{row.test}}</td>
<td> {{row.id.errors}} {{row.id}} </td>

</tr>
{% endfor %}</tbody></table>


</div>


</form>



Thank you.


Reply all
Reply to author
Forward
0 new messages