Ticket #15244: doc-custom_renderer_howto.patch

File doc-custom_renderer_howto.patch, 2.1 KB (added by lkeijser, 14 years ago)

patch to include custom renderer documentation

  • docs/howto/custom-widget-renderer.txt

    diff -ruN -x .svn django-trunk-orig/docs/howto/custom-widget-renderer.txt django-trunk/docs/howto/custom-widget-renderer.txt
    old new  
     1============================================
     2Writing (and using) a custom widget renderer
     3============================================
     4
     5It's possible to override a renderer for a specific widget by subclassing
     6it. A good example is the RadioSelect widget that, by default, outputs
     7a list of items.
     8
     9To override this behavior, we need to subclass the widget. In particular
     10its renderer function. For example, take this piece of code that can be
     11included in ``myproject/myapp/forms.py``:
     12
     13.. code-block:: python
     14    from django.forms.widgets import RadioFieldRenderer, RadioSelect
     15    from django.utils.encoding import StrAndUnicode, force_unicode
     16    from django.utils.safestring import mark_safe
     17   
     18    class CustomRenderer(RadioFieldRenderer):
     19        def render(self):
     20            return mark_safe(u'\n%s\n' % u'\n'.join([u'<tr><td>%s</td></tr>'
     21                    % force_unicode(w) for w in self]))
     22   
     23    class CustomRadioSelect(RadioSelect):
     24        renderer = CustomRenderer
     25
     26
     27You can then use the new widget like this:
     28
     29.. code-block::  python
     30    class OSChoiceForm(forms.Form):
     31        OS_CHOICES = (
     32                ('linux', 'Linux'),
     33                ('windows', 'Windows'),
     34                ('mac', 'Macintosh'),
     35                )
     36        os = forms.ChoiceField(label='', widget=CustomRadioSelect, choices=OS_CHOICES )
     37
     38And the form will be rendered as table rows instead of list items.
     39
  • docs/howto/index.txt

    diff -ruN -x .svn django-trunk-orig/docs/howto/index.txt django-trunk/docs/howto/index.txt
    old new  
    1515   custom-model-fields
    1616   custom-template-tags
    1717   custom-file-storage
     18   custom-widget-renderer
    1819   deployment/index
    1920   error-reporting
    2021   initial-data
Back to Top