UnicodeWarning:Unicode等比较无法将两个参数转换为Unicode [英] 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- package / django / core / files / base.py:106:UnicodeWarning:Unicode等比较无法将两个参数转换为Unicode - 将它们解释为不相等
如果('\\\
', '\r'):
代码很简单。这是在我的views.py
The code is pretty straightforward. This is in my views.py
def my_image(request):
clothes = Clothes.objects.get(clothesid = '2')
get_image = clothes.image
return HttpResponse(get_image, mimetype="image/png")
这是在我的models.py
And this is in my models.py
image = models.FileField(upload_to='images/')
完整的衣服
模型看起来像这样:
The complete Clothes
model looks like this:
class Clothes(models.Model):
clothesid = models.IntegerField(primary_key=True)
clothessize = models.CharField(max_length=255L, blank=True)
clothescolour = models.CharField(max_length=255L, blank=True)
clothestype = models.CharField(max_length=255L, blank=True)
image = models.FileField(upload_to='images/')
url = models.CharField(max_length=255L, blank=True)
gender = models.CharField(max_length=1L, blank=True)
clothescategory = models.CharField(max_length=255L, blank=True)
clothesage = models.CharField(max_length=255L, blank=True)
class Meta:
db_table = 'Clothes'
def __unicode__(self):
return self.image.name
在MySQL中,图像字段整理为 latin1_swedish_ci
,以防出现问题。
In MySQL, the image field is collated as latin1_swedish_ci
in case that is an issue.
这里有什么问题?
推荐答案
您传递了 FileField
image
直接进入响应,就好像它是一个字符串或迭代。检查源代码以获取异常路径,我们看到该字段返回的对象在其 __ iter __
方法中有违规行 - 包装类正在寻找行终止符。原始图像文件可能包含无法转换为可与行终止符字符进行比较的字节,这显然是合理的。
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.
HttpResponse
只需要一些它可以视为一个字符串的东西 - 如果你给它一个迭代器,它一次读取它,并创建一个字符串,所以没有可用的内存可用:
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将立即使用迭代器,将其内容存储为字符串,并将其丢弃。
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.
您也可以尝试简要介绍让托管网页服务器处理图片的简单情况,只需将重定向到 FileField URL >。这将涉及一个额外的HTTP往返告诉浏览器在哪里,所以我不认为有一个通用的规则,哪种方法会更快。
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.
这篇关于UnicodeWarning:Unicode等比较无法将两个参数转换为Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!