Tutorial :Inserting A Heading Into A Django Form



Question:

I would like my form to output something like this:

Name: ___________  Company: ___________    Interested in  -------------  Foo: [ ]  Bar: [ ]  Baz: [ ]  

That is to say, I would like to have the 'Interested In' title inserted in the middle of my form output.

One way of doing this would be to write out the template code for each field, and insert the heading as appropriate. However, I'd prefer not to do this. I suspect that there is something to do with formsets that will do what I want.

Is there a way to do what I want, or do I just need to suck it up and accept that I need to write my template out longhand for this?


Solution:1

With FieldsetMixin you get Admin-like fieldsets. You create your form like this:

from django.forms import Form  from formfieldset.forms import FieldsetMixin      class MyForm(Form, FieldsetMixin):      fieldsets = (          (u'', {'fields': ['name', 'company']}),          (u'Interested in', {'fields': ['foo', 'bar', 'baz']}),      )        # rest of the form  


Solution:2

Django formsets are not the way to go: They are used if you need to edit multiple objects at the same time. HTML fieldsets are more like the correct way. I imagine you could group the name and company fields into one fieldset, and then the interest fields into another.

On DjangoSnippets you can find a template tag that aids the grouping.

Using this snippet, you would define the grouping in your view as

fieldsets = (      ('', {'fields': ('name','company')}),      ('Interested in', {'fields': ('foo','bar','baz')})  )  

Pass it to the template along with the form, and render the form with

{% draw_form form fieldsets %}  

I would prefer not to define the grouping in the view, as it's mainly presentational issue and should belong to the view, but what the heck, this seems to do the job!

muhuk's solution though looks more elegant than this...


Solution:3

The form fields retain the order with which you defined the form class or model if it's form for model. In the template you can iterate thru all the fields specifying how they are rendered, you can check for the one you are looking for with the ifequal tag and specify the different rendering.

See:

http://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#looping-over-the-form-s-fields


Solution:4

It's a bit of a hack, but you could define a HeaderWidget that just renders its value:

from django.forms.util import flatatt  from django.utils.html import format_html    class HeaderWidget(forms.widgets.Widget):      def render(self, name, value, attrs=None):          attrs.update(self.attrs)          return format_html('<div{0}>{1}</div>', flatatt(attrs), value)  

and then add a field in the desired spot using that widget:

header = forms.CharField(      widget=HeaderWidget(attrs={'class': 'my-css-class'}),      initial='Interested in',      required=False,      label='')  

A minor downside is that you'll get the header "value" posted with your form.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »