Snippet List
You need jQuery and Bootstrap 2 and bootstrap-dropdown.js. Apart from that, this should be a perfect drop-in replacement for the normal select widget. A jQuery event callback maintains a hidden input field from the user's selection. Upon loading the page, the hidden input field is set.
The SelectWidgetBootstrap also contains a <noscript> tag containing the normal forms.Select widget.
Example usage:
class MyForm(forms.Form):
group = forms.ModelChoiceField(models.Group.objects.all(), widget=SelectWidgetBootstrap(),
empty_label=_(u'(no group selected)'))
- dropdown
- select
- bootstrap
This widget can be imported in your forms.py file and used like so:
formfield = forms.ModelChoiceField(widget=SelectDropdownWidget, queryset=Model.objects.all())
The javascript is provided by [filament group's Scott and his jQueryUI Selectmenu](http://www.filamentgroup.com/lab/jquery_ui_selectmenu_an_aria_accessible_plugin_for_styling_a_html_select/). In addition to the javascript you will need [jQuery (the latest release is fine) and jQuery UI](http://jqueryui.com/). The convenient thing is that this widget will style to match whichever jQuery UI you've 'rolled' or selected, making it highly customizable!
Hopefully this helps others who wanted a nice widget with jQuery and were sad to find there were no nice default options, enjoy! :)
- dropdown
- jquery
- select
- widget
- jqueryui
Using newforms you can create forms from existing models easily and automatically with either `form_for_model(class)` or `form_for_instance(instance)`. Usually the automagically generated form fields are sufficient; however, sometimes you need to restrict selections in a choice field.
You can also set the default selection when instantiating the form. In this example, if `acct` is not contained in the 'account' field choices, the selection defaults to the first entry.
This example is probably not good practice when using `form_for_instance` because the existing value 'selection' of the choice field is lost and must be reset manually (see above).
- newforms
- foreignkey
- dropdown
- choicefield
- form_for_model
- form_for_instance
Most of the time when you want a dropdown selector based on
a ForeignKey, you'll want to use [snippet #26](http://www.djangosnippets.org/snippets/26/)
Here's an alternative approach, perhaps useful when you want to define choices once and reuse it in different views without overriding Form `__init__`.
- dynamic
- foreignkey
- dropdown
- choices
- property
5 snippets posted so far.