Code

#18407 closed Bug (fixed)

Validation error messages should support unicode params

Reported by: znotdead Owned by: claudep
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'

Attachments (0)

Change History (2)

comment:1 Changed 23 months ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to claudep
  • Patch needs improvement unset
  • Summary changed from 500 instead of 404 when unicode in url to Validation error messages should support unicode params
  • Triage Stage changed from Unreviewed to Accepted

AutoField doesn't seem the only field concerned.

comment:2 Changed 23 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.