Skip to content

maraujop/django-MultiWidgetLayout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

django-MultiWidgetLayout

Usage

This is an enhanced class based on MultiWidget that lets you control the rendering of a subclass using a layout specified by a list. Let's see an example first:

class AddressWidget(MultiWidgetLayout):
    def __init__(self, attrs=None):
        layout = [ 
            "<label for='%(id)s'>Street:</label>", TextInput()
            "<label for='%(id)s'>Number:</label>", TextInput(),
            "<label for='%(id)s'>Zip Code:</label>", TextInput()
        ]
        super(AddressWidget, self).__init__(layout, attrs)

    def decompress(self, value):
        if value:
            return value.split(",")
        return [None, None, None]

This is what you get:

<label for='id_address_field_0'>Street:</label>
<input id="id_address_field_0" type="text" name="address_field_0" class="addresswidget" />
<label for='id_address_field_1'>Number:</label>
<input id="id_address_field_1" type="text" name="address_field_1" class="addresswidget" />
<label for='id_address_field_2'>Zip Code:</label>
<input id="id_address_field_2" type="text" name="address_field_2" class="addresswidget" />

Compared to MultiWidget, the differences are:

  • widgets parameter no longer exists.
  • layout The layout is a list of strings and widgets that will be rendered. They are rendered in that exact order. String will be formatted using the dictionary attrs.

Bugs or Suggestions

This is an experiment to improve the rendering of MultiWidgets. I have openned a ticket in Django (#16959) to see if the concept is accepted, meanwhile you can use this. If you have suggestions or you come across bugs, please open an issue.

About

An enhanced class based on MultiWidget that lets you easily control the rendering using a layout

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages