From dcfd378fbca31eedbe3da3f35bb7a3ba2ec11778 Mon Sep 17 00:00:00 2001 From: Mike Ryan Date: Wed, 21 Mar 2012 18:54:59 +0000 Subject: [PATCH] Added get_layout function. This makes dynamic layouts a bit easier when working with form inheritance. --- bootstrap/forms.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/bootstrap/forms.py b/bootstrap/forms.py index 681df1b..ab5f3af 100755 --- a/bootstrap/forms.py +++ b/bootstrap/forms.py @@ -14,13 +14,6 @@ class BootstrapMixin(object): def __init__(self, *args, **kwargs): super(BootstrapMixin, self).__init__(*args, **kwargs) - # do we have an explicit layout? - if hasattr(self, 'Meta') and hasattr(self.Meta, 'layout'): - self.layout = self.Meta.layout - else: - # Construct a simple layout using the keys from the fields - self.layout = self.fields.keys() - if hasattr(self, 'Meta') and hasattr(self.Meta, 'custom_fields'): self.custom_fields = self.Meta.custom_fields else: @@ -39,13 +32,23 @@ def top_errors_as_html(self): return ''.join(["
%s
" % error for error in self.top_errors]) + def get_layout(self): + """ Return the user-specified layout if one is available, otherwise + build a default layout containing all fields. + """ + if hasattr(self, 'Meta') and hasattr(self.Meta, 'layout'): + return self.Meta.layout + else: + # Construct a simple layout using the keys from the fields + return self.fields.keys() + def as_div(self): """ Render the form as a set of
s. """ self.top_errors = self.non_field_errors() self.prefix_fields = [] - output = self.render_fields(self.layout) + output = self.render_fields(self.get_layout()) if self.top_errors: errors = self.top_errors_as_html()