Dynamically change a form select widget to a hidden widget

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""Change a ChoiceField select widget into a hidden field if the right GET variable is passed"""

""" code for your view """
designation  = request.GET.get('d')
choice       = {}

if not designation:
	form = Form()
else:
	"""Setup the designation field as a hidden field if the right get variable was passed"""
	
	form = Form(initial={ 'designation': designation })
	from django.forms import widgets

	for item in form.fields['designation'].choices:
		if str(item[0]) == designation:
			form.fields['designation'].widget = widgets.HiddenInput()
			choice['designation'] = str(item[1])

return render_to_response('my-form.html', { 'form': form, 'choice': choice }, context_instance=RequestContext(request))


""" code for your template """
<p>{{ form.designation.label_tag }}{% if choice.designation %}{{ choice.designation }}{% endif %}{{ form.designation }}</p>

More like this

  1. Hidden Forms by insin 5 years, 11 months ago
  2. Bootstrap button dropdown widget (replaces forms.Select) by benjaoming 10 months, 3 weeks ago
  3. load m2m fields objects by dirol 3 years ago
  4. Radio widget with labels after inputs by avsd 11 months, 2 weeks ago
  5. "Dynamic" form with a hidden field by flaxter 4 years, 2 months ago

Comments

(Forgotten your password?)