Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mezzanine -- 'max_digits' must be great or equal to 'decimal_places' on OpenBSD/'ascii' codec can't encode characters in position 58-61: ordinal not in range/Python修改編碼 #216

Open
maxchen989 opened this issue Jul 5, 2023 · 0 comments

Comments

@maxchen989
Copy link
Owner

Mezzanine -- 'max_digits' must be great or equal to 'decimal_places' on OpenBSD/'ascii' codec can't encode characters in position 58-61: ordinal not in range/Python修改編碼

最近在部署Mezzanine時, 遇到了這個問題.

這是Mezzanine其中的一個app所引起的error.

目前找到的解法如下

local_setting.py中加入

SHOP_CURRENCY_LOCALE = 'en_US.UTF-8'

不過這會導致, 後續連結Database造成編碼問題.

blog的某些post會有error:

`'ascii' codec can't encode characters in position 58-61: ordinal not in range(128)`

尚未釐清


後記:

我比對了Local 正常運作的環境與DigitalOcean的環境, 並將其同步為
註:vir是python virtualenv, sys則是系統編碼

```
local-

sys:
locale : ('Chinese (Traditional)_Taiwan', '950')
default : utf-8

vir:
locale : none
default : utf-8

digital-

sys:
local : en_US, UTF-8
default : utf-8

vir:
locale : none
default : utf-8

```

同步語法:

```
#檢查python encode
a='haha'
print(chardet.detect(a.encode()))

#設定default
export PYTHONIOENCODING=utf-8
#檢查default
python
import sys
print(sys.getdefaultencoding())
#檢查filesystemencoding
print(sys.getfilesystemencoding())

#設定locale
export LC_ALL=en_US.UTF-8 #在Virtual不起作用

python
import locale
locale.setlocale(locale.LC_ALL, '<language string>')

```

同步到最後找不到此項編碼:
Chinese (Traditional)_Taiwan.950

因此最後決定就維持目前這樣.

經測試發現, Tile中有中文的post都會報ascii錯誤, 而文章內容有中文的不會影響.

並且發現如果一開始tile設定為英文或是數字, 就算之後再改為中文也不會報錯.

調查後發現, 是與django抓取url有關.

原本tile是"123", 運作正常, 後改為"更改後"也正常.

但如果原本標題直接用"更改後"送出, 則Django會去抓"更改後".html.

問題出在這裡!

如何讓Django處理utf8 url.


但在經過測試, 如果不使用apache轉送, 直接使用manage.py runserver是不會有以上問題的.

所以原因就是apache的encoding問題!

去/etc/apache2/envvars,加入以下兩個設定, 此問題就解決了

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

折騰好久!

參考來源:

stephenmcd/cartridge#330
https://docs.oracle.com/cd/E19683-01/816-3982/utf8-98/index.html
https://blog.longwin.com.tw/2018/09/python3-unicode-encode-error-ascii-codec-2018/
https://stackoverflow.com/questions/1629699/locale-getlocale-problems-on-osx
https://ephrain.net/linux-%E7%92%B0%E5%A2%83%E8%AE%8A%E6%95%B8-lang-lc_-lc_all-%E5%B0%8D-l10n-gettext-%E7%9A%84%E5%BD%B1%E9%9F%BF/
https://blog.csdn.net/sinat_24648637/article/details/84190482
stephenmcd/cartridge#164
https://docs.djangoproject.com/zh-hans/3.2/howto/deployment/wsgi/modwsgi/

tags: Mezzanine Apache Django
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant