Login

"Dynamic" form with a hidden field

Author:
flaxter
Posted:
March 25, 2009
Language:
Python
Version:
1.0
Tags:
dynamic form field hidden
Score:
1 (after 1 ratings)

This code demonstrates two simple techniques:

  1. so-called "dynamic" forms, in which the form is created at run time by the model

  2. using a widget (forms.widgets.HiddenInput) for a field of the form. I feel like this could be highlighted more in the documentation. You need to do something similar to get a textarea (forms.CharField(widget=forms.widgets.Textarea()) and it took me too long to figure this out.

There are, no doubt, good reasons not to do what I'm doing here the way I'm doing it. Peanut gallery?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from django import forms

hiddenValues = forms.Form()

value0 = 17

hiddenValues.fields['value0'] = forms.CharField(initial=value0, 
  widget=forms.widgets.HiddenInput())

# pass the form to your template
# render it as you like, e.g. with form.as_p

More like this

  1. Dynamically change a form select widget to a hidden widget by epicserve 6 years, 1 month ago
  2. Complex Formsets, Redux by smagala 5 years, 4 months ago
  3. Type checking templatetag filters by marcorogers 5 years, 5 months ago
  4. Dynamically add css-classes to formfields by Lacour 4 years ago
  5. Select or Create widget by Naster 1 year, 5 months ago

Comments

tommy (on August 5, 2010):

Great tip. Works like a charm.

#

Please login first before commenting.