Skip to content

Commit

Permalink
Added get_layout function.
Browse files Browse the repository at this point in the history
This makes dynamic layouts a bit easier when working with form
inheritance.
  • Loading branch information
mikery committed Mar 21, 2012
1 parent 520a59f commit dcfd378
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions bootstrap/forms.py
Expand Up @@ -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:
Expand All @@ -39,13 +32,23 @@ def top_errors_as_html(self):
return ''.join(["<div class=\"alert alert-error\">%s</div>" % 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 <div>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()
Expand Down

0 comments on commit dcfd378

Please sign in to comment.