Skip to content

Instantly share code, notes, and snippets.

@chintanop
Created October 12, 2012 15:01
Show Gist options
  • Save chintanop/3879630 to your computer and use it in GitHub Desktop.
Save chintanop/3879630 to your computer and use it in GitHub Desktop.
semantic dynamic django forms
form_widget = forms.ChoiceField(label=disp_entity.label, widget=forms.CheckboxSelectMultiple(), required=disp_entity.is_required, choices=choices)
#We also created some custom widgets to display custom arbitrary HTML:
form_widget = forms.CharField(label='',widget=CustomHTMLWidget(attrs={'html':html}), required=False)
#Next we define a dynamic Django form by subclassing the original django.Forms then assign our custom display fields to this form.
class DynamicForm(forms.Form):
def __init__(self, display_fields):
for display_field in display_fields:
self.fields[display_field.key] = display_field
#For rendering rules and triggering them dynamically via Javascript,
#we created a custom widget in Django that is rendered for all rules applicable
#for the given form. Below the code snippet on how we associate a rule
rule_fields[rule_entity_webparam] = forms.CharField(label='',widget=OnOffRuleWidget(attrs={
'field':rule.if_source_display_entity.webparam,
'target_trigger_val':rule.has_target_value,
'toggle':sub_webparam,
'init_state': 'visible',
'rule_webparam': rule_entity_webparam}), required=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment