Opened 13 years ago

Closed 13 years ago

#14525 closed (invalid)

Bug - exception in messages.info(), etc when using unicode characters.

Reported by: Michael Angeletti Owned by: nobody
Component: Contrib apps Version: 1.2
Severity: Keywords: messages
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

On http://groups.google.com/group/django-developers/browse_thread/thread/2c3b0d0b518d0c2f I reported that Unicode values causes an exception when passed into messages.info() (and other 3 messages methods). In my report, I erroneously suggested that a Unicode character in the URL's GET parameters would cause the exception. The following example will reproduce the exception (includes extraneous steps for the sake of completion):

Example:

# The following assumes that settings are default, except the for SQLLite3 database setting, and file path, and the installed apps setting.

# Model
class Foo(Model):
    name = models.CharField(max_length=255)

# Form
class FooForm(ModelForm):
    class Meta:
        model = Foo

# View
def create_foo(request):
    if request.method == 'POST':
        form = FooForm(request.POST)
        if form.is_valid():
            instance = form.save()
            messages.info(request, 'Thanks, {0}.'.format(instance.name))
            return redirect('foo_thanks')
    else:
        form = FooForm()
    data = {
        'form': form,
    }
    context = RequestContext(request, data)
    template = Template("""<html>
            <head>
                <title>Bug Page</title>
            </head>
            <body>
                <div>
                    <h1>Type ° (alt + 0176) into the name field, and hit submit.</h1>
                    <form action="" method="post" enctype="application/x-www-form-urlencoded">{% csrf_token %}
                        {{form.as_p}}
                        <p>
                            <button type="submit">Raise Exception</button>
                        </p>
                    </form>
                </div>
            </body>
        </html>""")
    output = template.render(context)
    return HttpResponse(output)


def foo_thanks(request):
    return HttpResponse('No need to render message, as exception occurs upon creation.')

Traceback:

UnicodeEncodeError at /create_foo/

('ascii', u'\xb0', 0, 1, 'ordinal not in range(128)')

Request Method: 	GET
Request URL: 	http://127.0.0.1:8080/create_foo/
Django Version: 	1.3 pre-alpha SVN-14235
Exception Type: 	UnicodeEncodeError
Exception Value: 	

('ascii', u'\xb0', 0, 1, 'ordinal not in range(128)')

Exception Location: 	D:\myproject\myapp\views.py in create_foo, line XXX
Python Executable: 	C:\Python26\python.exe
Python Version: 	2.6.4

1) response = callback(request, *callback_args, **callback_kwargs)
2) messages.info(request, 'Thanks, {0}.'.format(instance.name))

There are only 2 steps in the traceback.

Change History (2)

comment:1 by Tobias McNulty, 13 years ago

I think the error might be in your code. See below:

In [4]: u'Thanks, {0}.'.format(u'°')
Out[4]: u'Thanks, \xc2\xb0.'

In [5]: 'Thanks, {0}.'.format(u'°')
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)

/home/tobias/<ipython console> in <module>()

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

comment:2 by Alex Gaynor, 13 years ago

Resolution: invalid
Status: newclosed

tobias is correct, the stacktrace ends right at your format call. Closing as invalid since there's no bug in Django.

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