Задать вопрос

Тел: +7 965 3737 888

487

Просмотров

3

Ответов

Form splitting/Fieldset templatetag

<p>Syntax: {% get_fieldset list,of,fields as new_form_object from original_form %}</p>
<p>Example:</p>
{% load fieldsets %}
...
&lt;fieldset id="contact_details"&gt;
    &lt;legend&gt;Contact details&lt;/legend&gt;
    &lt;ul&gt;
{% get_fieldset first_name,last_name,email,cell_phone as personal_fields from form %}
{{ personal_fields.as_ul }}
    &lt;/ul&gt;
&lt;/fieldset&gt;

&lt;fieldset&gt;
    &lt;legend&gt;Address details&lt;/legend&gt;
    &lt;ul&gt;
{% get_fieldset street_address,post_code,city as address_fields from form %}
{{ address_fields.as_ul }}
    &lt;/ul&gt;
&lt;/fieldset&gt;

Вопрос полезен? Да0/Нет0
file_2341.py(1.1Кб)
None

Ответы (3):

Ответbenjaoming:17.01.2012
Ответ полезен? Да0/Нет0

This is obviously a classic! Thanks a million :)

Ответczartur:08.08.2011
Ответ полезен? Да0/Нет0

I would personally recommend to change this:

    #new_form.fields = SortedDict(
        #[(key, value) for key, value in form.fields.items() 
         #if key in self.fields])

to that:

    new_form.fields = SortedDict(
        [(key, form.fields[key]) for key in self.fields]
    )

So you can preserve ordering in your forms.

Apart from that – Brilliant snippet, saved me ton of work.

Ответgregb:09.08.2009
Ответ полезен? Да0/Нет0

Thanks for this snippet - very handy. I created a similar "excluding" tag which works in the same way but excludes the listed fields - using the two together saves a bit of typing especially if you just want to split out a few fields from a long form into their own fieldset.