Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

newforms: Added __unicode__() methods wherever there were __str__() m…

…ethods, and changed the __str__() methods to delegate to __unicode__().encode(settings.DEFAULT_CHARSET)

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6c0219cf72dc8525284bde509cfac95d546f9783 1 parent a794acb
@adrianholovaty adrianholovaty authored
10 django/newforms/
@@ -6,7 +6,7 @@
from django.utils.html import escape
from fields import Field
from widgets import TextInput, Textarea, HiddenInput
-from util import ErrorDict, ErrorList, ValidationError
+from util import StrAndUnicode, ErrorDict, ErrorList, ValidationError
NON_FIELD_ERRORS = '__all__'
@@ -32,7 +32,7 @@ def __new__(cls, name, bases, attrs):
attrs['fields'] = SortedDictFromList(fields)
return type.__new__(cls, name, bases, attrs)
-class Form(object):
+class Form(StrAndUnicode):
"A collection of Fields, plus their associated data."
__metaclass__ = DeclarativeFieldsMetaclass
@@ -43,7 +43,7 @@ def __init__(self, data=None, auto_id=False): # TODO: prefix stuff
self.clean_data = None # Stores the data after clean() has been called.
self.__errors = None # Stores the errors after clean() has been called.
- def __str__(self):
+ def __unicode__(self):
return self.as_table()
def __iter__(self):
@@ -155,14 +155,14 @@ def clean(self):
return self.clean_data
-class BoundField(object):
+class BoundField(StrAndUnicode):
"A Field plus data"
def __init__(self, form, field, name):
self.form = form
self.field = field = name
- def __str__(self):
+ def __unicode__(self):
"Renders this field as an HTML widget."
# Use the 'widget' attribute on the field to determine which type
# of HTML widget to use.
15 django/newforms/
@@ -1,13 +1,22 @@
-# Default encoding for input byte strings.
-DEFAULT_ENCODING = 'utf-8' # TODO: First look at django.conf.settings, then fall back to this.
+from django.conf import settings
def smart_unicode(s):
if not isinstance(s, basestring):
s = unicode(str(s))
elif not isinstance(s, unicode):
- s = unicode(s, DEFAULT_ENCODING)
+ s = unicode(s, settings.DEFAULT_CHARSET)
return s
+class StrAndUnicode(object):
+ """
+ A class whose __str__ returns its __unicode__ as a bytestring
+ according to settings.DEFAULT_CHARSET.
+ Useful as a mix-in.
+ """
+ def __str__(self):
+ return self.__unicode__().encode(settings.DEFAULT_CHARSET)
class ErrorDict(dict):
A collection of errors that knows how to display itself in various formats.
10 django/newforms/
@@ -8,7 +8,7 @@
'Select', 'SelectMultiple', 'RadioSelect', 'CheckboxSelectMultiple',
-from util import smart_unicode
+from util import StrAndUnicode, smart_unicode
from django.utils.html import escape
from itertools import chain
@@ -146,7 +146,7 @@ def render(self, name, value, attrs=None, choices=()):
return u'\n'.join(output)
-class RadioInput(object):
+class RadioInput(StrAndUnicode):
"An object used by RadioFieldRenderer that represents a single <input type='radio'>."
def __init__(self, name, value, attrs, choice, index):, self.value = name, value
@@ -154,7 +154,7 @@ def __init__(self, name, value, attrs, choice, index):
self.choice_value, self.choice_label = choice
self.index = index
- def __str__(self):
+ def __unicode__(self):
return u'<label>%s %s</label>' % (self.tag(), self.choice_label)
def is_checked(self):
@@ -168,7 +168,7 @@ def tag(self):
final_attrs['checked'] = 'checked'
return u'<input%s />' % flatatt(final_attrs)
-class RadioFieldRenderer(object):
+class RadioFieldRenderer(StrAndUnicode):
"An object used by RadioSelect to enable customization of radio widgets."
def __init__(self, name, value, attrs, choices):, self.value, self.attrs = name, value, attrs
@@ -178,7 +178,7 @@ def __iter__(self):
for i, choice in enumerate(self.choices):
yield RadioInput(, self.value, self.attrs.copy(), choice, i)
- def __str__(self):
+ def __unicode__(self):
"Outputs a <ul> for this set of radio fields."
return u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>' % w for w in self])
Please sign in to comment.
Something went wrong with that request. Please try again.