Opened 12 years ago

Closed 12 years ago

#18407 closed Bug (fixed)

Validation error messages should support unicode params

Reported by: znotdead Owned by: Claude Paroz
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords: urls pk admin unicode
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Does not provided case when object_id is Unicode

Example:
http://localhost:8000/admin/auth/user/%D1%8B%D0%B2%D0%B0%D1%8B%D0%B2/

Gives error:

UnicodeEncodeError at /admin/auth/user/ываыв/

'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

Request Method: 	GET
Request URL: 	http://localhost:8000/admin/auth/user/%D1%8B%D0%B2%D0%B0%D1%8B%D0%B2/
Django Version: 	1.4
Exception Type: 	UnicodeEncodeError
Exception Value: 	

'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

Exception Location: 	/media/disk/env/django14/lib/python2.6/site-packages/django/db/models/fields/__init__.py in to_python, line 528
Python Executable: 	/media/disk/env/django14/bin/python
Python Version: 	2.6.7

Exception happened on AutoField(Field) to_python method:

msg = self.error_messages['invalid'] % str(value)

And so we have 500 but not 404.

/media/disk/env/django14/lib/python2.6/site-packages/django/contrib/admin/options.py in get_object

            Returns an instance matching the primary key provided. ``None``  is

            returned if no match is found (or the object_id failed validation

            against the primary key field).

            """

            queryset = self.queryset(request)

            model = queryset.model

            try:

                object_id = model._meta.pk.to_python(object_id)

    ...

                return queryset.get(pk=object_id)

            except (model.DoesNotExist, ValidationError):

                return None

        def get_changelist_form(self, request, **kwargs):

            """

▶ Local vars
/media/disk/env/django14/lib/python2.6/site-packages/django/db/models/fields/__init__.py in to_python

        def to_python(self, value):

            if value is None:

                return value

            try:

                return int(value)

            except (TypeError, ValueError):

                msg = self.error_messages['invalid'] % str(value)

    ...

                raise exceptions.ValidationError(msg)

        def validate(self, value, model_instance):

            pass

        def get_prep_value(self, value):

▼ Local vars
Variable 	Value
self 	<django.db.models.fields.AutoField: id>
value 	u'\u044b\u0432\u0430\u044b\u0432'

Change History (2)

comment:1 by Claude Paroz, 12 years ago

Owner: changed from nobody to Claude Paroz
Summary: 500 instead of 404 when unicode in urlValidation error messages should support unicode params
Triage Stage: UnreviewedAccepted

AutoField doesn't seem the only field concerned.

comment:2 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: newclosed

In [0dc904979d5b6df78662653d498c91f4d54f36c2]:

Fixed #18407 -- Made model field's to_python methods fully accept unicode.

When generating error message in to_python, any unicode string
containing non-ascii characters triggered a UnicodeEncodeError for
most field types.

Note: See TracTickets for help on using tickets.
Back to Top