Re: How to render formset in template django and create vertical table?
On Fri, Jul 5, 2013 at 4:39 PM, MacVictor <witold.gren@gmail.com> wrote:
> How to create this table used formset and extra=3?
>
> Table:
>
> | my name first column | second column | third column | fourth column |
>
> | formset1.field1.label | formset1.field1 | formset2.field1 |
> formset3.field1 |
>
> | formset1.field2.label | formset1.field2 | formset2.field2 |
> formset3.field2 |
>
> | formset1.field3.label | formset1.field3 | formset2.field3 |
> formset3.field3 |
>
> | formset1.field4.label | formset1.field4 | formset2.field4 |
> formset3.field4 |
>
> In view I create standard formset:
>
> def my_view(request, id_object):
> getobject = get_object_or_404(Model, id=id_object)
> FormSetInit = inlineformset_factory(Model, Model2, form=FormModel2,
> extra=3)
> FormSet = FormSetInit(instance=getobject, prefix='model2')
> response = {}
> response['Formset'] = FormSet
> return render_to_response('my_template.html', response)
>
>
> In template 'my_template.html' i try used this:
>
>
> <table>
> {% for form in Formset %}
> {% if forloop.counter == 1 %}
> {% for field in form %}
> {% if forloop.counter < 8 %}
> <tr>
> <td class="first">{{ field.label }}</td>
> <td>{{ field }}</td>
> <td>{{ field }}</td>
> <td class="last">{{ field }}</td>
> {% if forloop.counter != 7 %}
> </tr>
> {% endif %}
> {% else %}
> {{ field }}
> {% if forloop.last %} </tr> {% endif %}
> {% endif %}
> {% endfor %}
> {% endif %}
> {% endfor %}
> </table>
>
> But this not a good idea.. :/
Do the fields have fixed names?
<th> ... </th>
<tr>
{% for form in formset %}
<td>{{ form.field1.label }}</td><td>{{ form.field1 }}</td>
{% endfor
</tr>
<tr>
{% for form in formset %}
<td>{{ form.field2.label }}</td><td>{{ form.field2 }}</td>
{% endfor
</tr>
and so on.
For added points you could pass a list of field names in the order you
want them to appear, and use a 3rd party dict_get/attr_get template
tag to eliminate the duplication in the template. Something like this
should be in your toolkit:
http://push.cx/2007/django-template-tag-for-dictionary-access
Cheers
Tom
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home