Login

Snippets by scjody

Snippet List

Allow disabling options in a select widget

HTML allows an option in a <select> to be disabled. In other words it will appear in the list of choices but won't be selectable. This is done by adding a 'disabled' attribute to the <option> tag, for example: `<option value="" disabled="disabled">Disabled option</option>` This code subclasses the regular Django Select widget, overriding the render_option method to allow disabling options. Example of usage: class FruitForm(forms.Form): choices = (('apples', 'Apples'), ('oranges', 'Oranges'), ('bananas', {'label': 'Bananas', 'disabled': True}), # Yes, we have no bananas ('grobblefruit', 'Grobblefruit')) fruit = forms.ChoiceField(choices=choices, widget=SelectWithDisabled())

  • subclass
  • select
  • widget
Read More

scjody has posted 2 snippets.