在linux上django filebrowser 3.2中解码错误

时间:2021-07-17 07:22:58

The error message:

Traceback:
File "/web/hvita_perlan/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/web/hvita_perlan/lib/python2.6/site-packages/django/contrib/admin/views/decorators.py" in _checklogin
  19.             return view_func(request, *args, **kwargs)
File "/web/hvita_perlan/lib/python2.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  79.         response = view_func(request, *args, **kwargs)
File "/web/hvita_perlan/lib/python2.6/site-packages/filebrowser/views.py" in browse
  99.         fileobject = FileObject(os.path.join(file_dir, file))
File "/web/hvita_perlan/lib/python2.6/posixpath.py" in join
  70.             path += '/' + b

Exception Type: UnicodeDecodeError at /admin/filebrowser/browse/
Exception Value: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)

The string that could not be encoded/decoded was: /1h����.j

字符串不能编码/解码:/ 1 h����.

The file real filename is: 1hæð.jpg

文件的文件名是:1 hæð.jpg

some info:

> locale
LANG=en_GB.UTF-8

-

- - - - - -

> python manage.py shell
>>> import locale
>>> locale.getlocale()
('en_GB', 'UTF8')

>>> import os
>>> os.stat('../uploads/_promotional/1hæð_fb_thumb.jpg')
posix.stat_result(st_mode=33279, st_ino=788504L, st_dev=51713L, st_nlink=1, st_uid=0, st_gid=0, st_size=1629L, st_atime=1311176542, st_mtime=1311176542, st_ctime=1311177235)

As you can see everything works in shell but not in django filebrowser.

您可以看到,一切都在shell中工作,但在django文件浏览器中没有。

4 个解决方案

#1


1  

It appears from looking at the docs that FileBrowser only supports ASCII.

从查看文档来看,FileBrowser只支持ASCII。

It says in the exception:

它在例外中说:

Exception Value: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)

异常值:‘ascii’编解码器无法解码位置3:序数不在范围(128)的字节0xc3

os.path.join(file_dir, file) is getting a Unicode string, and it's being implicitly encoded to ASCII, rather than UTF-8. The unicode / string changes were made in Python 3 to remove this problem.

os.path。join(file_dir, file)得到一个Unicode字符串,它被隐式编码为ASCII,而不是UTF-8。Python 3中对unicode /字符串进行了更改,以消除这个问题。

Somewhere, file_dir needs to be encoded with file_dir.encode('utf-8'). As a bad hack to make it work, you could try doing it in /web/hvita_perlan/lib/python2.6/site-packages/filebrowser/views.py on line 99:

在某些地方,file_dir需要使用file_dir.encode(“utf-8”)进行编码。作为一种让它工作的糟糕方法,您可以尝试在/web/hvita_perlan/lib/python2.6/站点包/文件浏览器/视图中这样做。py 99行:

fileobject = FileObject(os.path.join(file_dir.encode('utf-8'), file))

And then test, and repeat every time you find a new spot in FileBrowser that triggers this error.

然后进行测试,每次在FileBrowser中发现一个新位置时重复此操作,该位置将触发此错误。

#2


1  

In django-filebrowser 3.5.6 there is a setting FILEBROWSER_NORMALIZE_FILENAME if set to true in your settings.py it will make fb strip non standard characters from the file name. I had trouble finding info about it so posting it here enven though not sure if it works for the older version.

在django-filebrowser 3.5.6中,设置文件browser_normalize_filename(如果在设置中设置为true)。它将使fb从文件名中去除非标准字符。我找不到关于它的信息,所以在这里发布它,尽管不确定它是否适用于旧版本。

#3


0  

The solution is here: http://diveintopython.net/xml_processing/unicode.html

解决方案如下:http://diveintopython.net/xml_processing/unicode.html

I solved the problem by adding sitecustomize.py to lib/python2.6/

我通过添加sitecustomize来解决这个问题。py lib / python2.6 /

# sitecustomize.py                   
# this file can be anywhere in your Python path,
# but it usually goes in ${pythondir}/lib/site-packages/
import sys
sys.setdefaultencoding('utf-8')

File browser files don't have the utf-8 header. I think they should change this. It looks like this:

文件浏览器文件没有utf-8头。我认为他们应该改变这一点。它看起来像这样:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

#4


0  

Its an old post but the issue remains.
I'm using django, apache2, django-filebrowser and get this Exception Value: 'ascii' codec can't decode byte 0xc3 in position...

这是一个老问题,但问题依然存在。我使用django, apache2, django-filebrowser,得到这个异常值:'ascii' codec不能解码字节0xc3的位置…

What worked for me even using mod_wsgi.

即使使用mod_wsgi,对我也有效。

#https://docs.djangoproject.com/en/1.2/howto/deployment/modpython/#if-you-get-a-unicodeencodeerror
#Put this in your apache2/envvars file.

export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

Hope it helps someone.

希望它能帮助一些人。

#1


1  

It appears from looking at the docs that FileBrowser only supports ASCII.

从查看文档来看,FileBrowser只支持ASCII。

It says in the exception:

它在例外中说:

Exception Value: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)

异常值:‘ascii’编解码器无法解码位置3:序数不在范围(128)的字节0xc3

os.path.join(file_dir, file) is getting a Unicode string, and it's being implicitly encoded to ASCII, rather than UTF-8. The unicode / string changes were made in Python 3 to remove this problem.

os.path。join(file_dir, file)得到一个Unicode字符串,它被隐式编码为ASCII,而不是UTF-8。Python 3中对unicode /字符串进行了更改,以消除这个问题。

Somewhere, file_dir needs to be encoded with file_dir.encode('utf-8'). As a bad hack to make it work, you could try doing it in /web/hvita_perlan/lib/python2.6/site-packages/filebrowser/views.py on line 99:

在某些地方,file_dir需要使用file_dir.encode(“utf-8”)进行编码。作为一种让它工作的糟糕方法,您可以尝试在/web/hvita_perlan/lib/python2.6/站点包/文件浏览器/视图中这样做。py 99行:

fileobject = FileObject(os.path.join(file_dir.encode('utf-8'), file))

And then test, and repeat every time you find a new spot in FileBrowser that triggers this error.

然后进行测试,每次在FileBrowser中发现一个新位置时重复此操作,该位置将触发此错误。

#2


1  

In django-filebrowser 3.5.6 there is a setting FILEBROWSER_NORMALIZE_FILENAME if set to true in your settings.py it will make fb strip non standard characters from the file name. I had trouble finding info about it so posting it here enven though not sure if it works for the older version.

在django-filebrowser 3.5.6中,设置文件browser_normalize_filename(如果在设置中设置为true)。它将使fb从文件名中去除非标准字符。我找不到关于它的信息,所以在这里发布它,尽管不确定它是否适用于旧版本。

#3


0  

The solution is here: http://diveintopython.net/xml_processing/unicode.html

解决方案如下:http://diveintopython.net/xml_processing/unicode.html

I solved the problem by adding sitecustomize.py to lib/python2.6/

我通过添加sitecustomize来解决这个问题。py lib / python2.6 /

# sitecustomize.py                   
# this file can be anywhere in your Python path,
# but it usually goes in ${pythondir}/lib/site-packages/
import sys
sys.setdefaultencoding('utf-8')

File browser files don't have the utf-8 header. I think they should change this. It looks like this:

文件浏览器文件没有utf-8头。我认为他们应该改变这一点。它看起来像这样:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

#4


0  

Its an old post but the issue remains.
I'm using django, apache2, django-filebrowser and get this Exception Value: 'ascii' codec can't decode byte 0xc3 in position...

这是一个老问题,但问题依然存在。我使用django, apache2, django-filebrowser,得到这个异常值:'ascii' codec不能解码字节0xc3的位置…

What worked for me even using mod_wsgi.

即使使用mod_wsgi,对我也有效。

#https://docs.djangoproject.com/en/1.2/howto/deployment/modpython/#if-you-get-a-unicodeencodeerror
#Put this in your apache2/envvars file.

export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

Hope it helps someone.

希望它能帮助一些人。