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修改編碼

Posted by: Max Chen | in Mezzanine | 1 year, 11 months ago |

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'
```

折騰好久!

參考來源:

https://github.com/stephenmcd/cartridge/issues/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 https://github.com/stephenmcd/cartridge/issues/164 https://docs.djangoproject.com/zh-hans/3.2/howto/deployment/wsgi/modwsgi/

tags: Mezzanine Apache Django
Currently unrated
 or 

Subscribe

* indicates required

Recent Posts

Archive

2023
2022
2021

Categories

Apache 1

Data Science 2

Dbfit 1

Design Pattern 1

Devops 4

DigitalOcean 1

Django 1

English 3

Excel 5

FUN 4

Flask 3

Git 1

HackMD 1

Heroku 1

Html/Css 1

Linux 4

MDX 1

Machine Learning 2

Manufacture 1

Master Data Service 1

Mezzanine 18

Oracle 1

Postgresql 7

PowerBI 4

Powershell 4

Python 22

SEO 2

SQL Server 53

SQL Server Analytics Service 1

SQLite 1

Windows 1

database 8

work-experience 1

其他 1

投資入門 1

投資心得 2

時間管理 1

總體經濟 2

自我成長 3

資料工程 1

Tags

SEO(1) Github(2) Title Tag(2) ML(1) 李宏毅(1) SQL Server(18) Tempdb(1) SSMS(1) Windows(1) 自我成長(2) Excel(1) python Flask(1) python(5) Flask(2)

Authors

Max Chen (159)

Feeds

RSS / Atom

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修改編碼

© COPYRIGHT 2011-2022. Max的文藝復興. ALL RIGHT RESERVED.