【问题标题】:UnicodeDecodeError when I set recursive=True in FilePathField in django当我在 django 的 FilePathField 中设置 recursive=True 时出现 UnicodeDecodeError
【发布时间】:2014-11-02 20:08:50
【问题描述】:

当我在管理员中添加记录时发生错误。我的模型是这样的

class THMusic(models.Model):
     originmusic = models.ForeignKey(Originalmusic)
     name = models.CharField(max_length=100)
     audiofile = models.FilePathField(path=os.path.dirname(os.path.abspath(__file__))+'/resource', recursive=True)
     team = models.CharField(max_length=50)
     album = models.CharField(max_length=50)
class THMusicAdmin(admin.ModelAdmin):
     list_display = ('name', 'originmusic', )

但我得到 UnicodeDecodeError:

UnicodeDecodeError at /admin/thmusic/thmusic/add/

'ascii' codec can't decode byte 0xe3 in position 54: ordinal not in range(128)

Request Method:     GET
Request URL:    http://127.0.0.1:8006/admin/thmusic/thmusic/add/
Django Version:     1.6.5
Exception Type:     UnicodeDecodeError
Exception Value:    

'ascii' codec can't decode byte 0xe3 in position 54: ordinal not in range(128)

Exception Location:     /usr/local/lib/python2.7/dist-packages/django/forms/fields.py in __init__, line 1057
Python Executable:  /usr/bin/python2.7
Python Version:     2.7.6
Python Path:    

['/home/hyzhappy/djangoprojects/myblog',
 '/usr/local/lib/python2.7/dist-packages/simplejson-3.6.3-py2.7-linux-i686.egg',
 '/home/hyzhappy/djangoprojects/myblog',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-i386-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/local/lib/python2.7/dist-packages/PIL',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gst-0.10',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']

如果我在 FilePathField 中删除“recursice=True”,它会顺利进行。但这不是我的期望 更多细节

Unicode 错误提示 无法编码/解码的字符串是:urce/������ UnicodeDecodeError('ascii', '/home/hyzhappy/djangoprojects/myblog/thmusic/resource/\xe3\x83\x97\xe3\x83\xac\xe3\x82\xa4\xe3\x83\xa4\xe3\x83\xbc \xe3\x82\xba\xe3\x82\xb9\xe3\x82\xb3\xe3\x82\xa2.mp3', 54, 55, '序数不在范围内(128)')

【问题讨论】:

  • 请包含错误的完整追溯。
  • 根据 Django 的文档,recursive 参数“包含”您提供的路径中的子目录。默认值为假。可能是其中一个子目录的字符不是 ascii 吗?
  • 你为什么有os.path.dirname(os.path.abspath(__file__))+'/resource'x + "/y"dirname 将是 x
  • 我没有子目录。并且 os.path.dirname(os.path.abspath(file))+'/resource' 确实有效。
  • 嗯,当然可以,但它的作用与os.path.abspath(___file___) 相同。

标签: python django


【解决方案1】:

可能不是正确的答案,但您可以将其作为可能的原因进行检查。

有时在 Django 中(我也遇到了同样的情况),UnicodeDecodeError 发生是因为初始的不相关错误试图显示但由于奇怪的字符而无法显示。

就我而言,这是因为我在代码 cmets(而不是 ascii)中加入了法语口音。这通常不会产生错误,但是当某处实际出现错误并且 Django 尝试在调试模式下显示此错误时,它不能因为非 ascii 字符而引发 UnicodeDecodeError 而不是引发初始错误。

只需尝试删除代码中的任何可疑字符(如果有的话),然后再次检查错误是否仍然相同。

【讨论】:

  • 我的代码或评论中没有奇怪的字符。我的数据库中有奇怪的字符。如您所见,我有一个 FilePathField,其中包含许多文件名。而且这些文件名可能有奇怪的字符。
【解决方案2】:

在后台,FilePathField 使用 os.walk/os.listdir 来查找文件。这些函数在 python 中有两种操作“模式”:字节串和 unicode。模式由给定的路径类型选择,即如果你给它一个unicode字符串,结果文件列表将为unicode,否则它将是一个字节字符串。

所以,要解决这个问题,只需提供一个指向FilePathField 的unicode 路径,如下所示(注意u'/resource' 开头的u):

audiofile = models.FilePathField(path=os.path.dirname(os.path.abspath(__file__))+u'/resource', recursive=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多