mirror of
https://github.com/django/django.git
synced 2025-10-30 00:56:09 +00:00
Split django.newforms into forms, fields, widgets, util. Also moved unit tests from docstrings to a standalone module in tests/regressiontests/forms, to save docstring memory overhead, keep code readable and fit our exisitng convention
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3945 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
43
django/newforms/widgets.py
Normal file
43
django/newforms/widgets.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""
|
||||
HTML Widget classes
|
||||
"""
|
||||
|
||||
__all__ = ('Widget', 'TextInput', 'Textarea', 'CheckboxInput')
|
||||
|
||||
from django.utils.html import escape
|
||||
|
||||
# Converts a dictionary to a single string with key="value", XML-style.
|
||||
# Assumes keys do not need to be XML-escaped.
|
||||
flatatt = lambda attrs: ' '.join(['%s="%s"' % (k, escape(v)) for k, v in attrs.items()])
|
||||
|
||||
class Widget(object):
|
||||
def __init__(self, attrs=None):
|
||||
self.attrs = attrs or {}
|
||||
|
||||
def render(self, name, value):
|
||||
raise NotImplementedError
|
||||
|
||||
class TextInput(Widget):
|
||||
def render(self, name, value, attrs=None):
|
||||
if value is None: value = ''
|
||||
final_attrs = dict(self.attrs, type='text', name=name)
|
||||
if attrs:
|
||||
final_attrs.update(attrs)
|
||||
if value != '': final_attrs['value'] = value # Only add the 'value' attribute if a value is non-empty.
|
||||
return u'<input %s />' % flatatt(final_attrs)
|
||||
|
||||
class Textarea(Widget):
|
||||
def render(self, name, value, attrs=None):
|
||||
if value is None: value = ''
|
||||
final_attrs = dict(self.attrs, name=name)
|
||||
if attrs:
|
||||
final_attrs.update(attrs)
|
||||
return u'<textarea %s>%s</textarea>' % (flatatt(final_attrs), escape(value))
|
||||
|
||||
class CheckboxInput(Widget):
|
||||
def render(self, name, value, attrs=None):
|
||||
final_attrs = dict(self.attrs, type='checkbox', name=name)
|
||||
if attrs:
|
||||
final_attrs.update(attrs)
|
||||
if value: final_attrs['checked'] = 'checked'
|
||||
return u'<input %s />' % flatatt(final_attrs)
|
||||
Reference in New Issue
Block a user