"""
File: widgets/__init__.py
HTML4 Widget overrides
"""
from django.newforms.widgets import Input
from django.utils.safestring import mark_safe
from django.newforms.util import flatatt
from django.utils.encoding import StrAndUnicode, force_unicode
class HTML4Input(Input):
input_type = None # Subclasses must define this.
def render(self, name, value, attrs=None):
if value is None: value = ''
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
if value != '':
# Only add the 'value' attribute if a value is non-empty.
final_attrs['value'] = force_unicode(value)
return mark_safe(u'' % flatatt(final_attrs))
class HTML4TextInput(HTML4Input):
input_type = 'text'
class HTML4PasswordInput(HTML4Input):
input_type = 'password'
class HTML4HiddenInput(HTML4Input):
input_type = 'hidden'
is_hidden = True
class HTML4FileInput(HTML4Input):
input_type = 'file'
needs_multipart_form = True
class HTML4DateTimeInput(HTML4Input):
input_type = 'text'
format = '%Y-%m-%d %H:%M:%S' # '2006-10-25 14:30:59'
"""
File: myapp/forms.py
Forms based on HTML4 widgets
"""
from django import newforms as forms
from myproject import widgets
class ThreadForm(forms.Form):
subject = forms.CharField(
max_length=150,
widget=widgets.HTML4TextInput(attrs={'size': 60}),
help_text="Something descriptive, maximum of 150 characters.")
message = forms.CharField(
widget=forms.Textarea(attrs={'cols': 60, 'rows': 10}),
help_text="Use of Markdown syntax is available, no HTML tags allowed.")