Skip to content Skip to sidebar Skip to footer

UnicodeWarning: Unicode Equal Comparison Failed To Convert Both Arguments To Unicode

This is the complete error that I am getting when I go to the relevant view. /Library/Python/2.7/site-packages/django/core/files/base.py:106: UnicodeWarning: Unicode equal comparis

Solution 1:

You're passing the value returned by the FileField image directly into the response as if it were a string or iterable. Checking the source code for the exception path given, we see that the object returned by the field has the offending line in its __iter__ method - the wrapper class is looking for line terminators. It's certainly plausible that the raw image file could contain bytes that can't be converted to something that can be compared against the line terminator characters.

The HttpResponse just needs something it can treat as a string - if you give it an iterator it reads it all in at once and creates a string, so there are no memory savings available:

HttpResponse will consume the iterator immediately, store its content as a string, and discard it.

https://docs.djangoproject.com/en/dev/ref/request-response/#passing-iterators

So you need something that will pull the contents of your image file wrapper object without going through the iteration interface. The read method does that, pulling in the file's entire content if you don't give it a number of bytes argument. Thus, the first thing I'd try is:

return HttpResponse(get_image.read(), mimetype="image/png")

This is untested, so I might have overlooked something.

You might also try to profile the simpler case of letting your hosting web server handle the images, and just serving a redirect to the URL returned from the FileField. That would involve an additional HTTP round trip to tell the browser where to look, so I don't think there's a universal rule for which approach will be faster.


Post a Comment for "UnicodeWarning: Unicode Equal Comparison Failed To Convert Both Arguments To Unicode"