首页 > 解决方案 > 尝试访问路径中包含 unicode 字符的文件,但不知何故,当前语言环境不支持 utf-8

问题描述

我使用django 夹层平台设计了一个网站,它完全没问题。我只是对utf-8 数字有疑问我在页面中使用的(实际上是波斯数字)。你看,在 django 模板中它将数字显示为英文数字,​​当我想以波斯语(utf-8)显示数字时,我必须使用自定义模板标签,它将所有数字转换为波斯语。(我提供了代码)。但在夹层模板中,它还必须使用另一个自定义模板标签,该标签在 WYSIWYG 编辑器中实现 html 标签。我的问题是当我想组合这两个模板标签时它会中断。特别是当页面中有图像时。考虑“richtext_filters”使用字符串提供的宽度和高度,并且在到达它们之前的数字已更改为utf-8位,然后出现我作为标题输入的错误:

'ascii' codec can't encode character '\u06f1' in position 83: ordinal not in range(128)

或其他错误:

Access was attempted on a file that contains unicode characters in its path, but somehow the current locale does not support utf-8. You may need to set 'LC_ALL' to a correct value, eg: 'en_US.UTF-8'.

我还考虑在我的共享主机提供商上更改“语言环境”,但也没有任何线索。感谢任何帮助。


需要的任何代码都在这里:我的自定义模板标签:

# -*- coding: utf-8 -*-
from django import template
from datetime import datetime

@register.filter(name='persianize_digits')
def persian_int(string):
    persianize = dict(zip("0123456789",'۰۱۲۳۴۵۶۷۸۹'))
    return ''.join(persianize[digit] if digit in persianize else digit for digit in str(string))

还有模板:

{{ page.richtextpage.content|persianize_digits|richtext_filters }}

如果你想知道“richtext_filters”,这里是代码:(mezzanine.utils.html

        width = img.get("width")
        height = img.get("height")
        if src_in_media and str(width).isdigit() and str(height).isdigit():
            img["src"] = settings.MEDIA_URL + thumbnail(src, width, height)

标签: pythondjangoutf-8mezzanine

解决方案


推荐阅读