over 4 years ago

在使用 django 檔案上傳時遇到了編碼問題。

Traceback:
File "/var/www/.venv/local/lib/python2.7/site-packages/django/db/models/base.py" in save
  546.                        force_update=force_update, update_fields=update_fields)
File "/var/www/.venv/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
  624.                         values = [(f, None, (raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks]
File "/var/www/.venv/local/lib/python2.7/site-packages/django/db/models/fields/files.py" in pre_save
  250.             file.save(file.name, file, save=False)
File "/var/www/.venv/local/lib/python2.7/site-packages/django/db/models/fields/files.py" in save
  86.         self.name = self.storage.save(name, content)
File "/var/www/.venv/local/lib/python2.7/site-packages/django/core/files/storage.py" in save
  47.         name = self.get_available_name(name)
File "/var/www/.venv/local/lib/python2.7/site-packages/django/core/files/storage.py" in get_available_name
  73.         while self.exists(name):
File "/var/www/.venv/local/lib/python2.7/site-packages/django/core/files/storage.py" in exists
  243.         return os.path.exists(self.path(name))
File "./.venv/lib/python2.7/genericpath.py" in exists
  18.         os.stat(path)

Exception Type: UnicodeEncodeError at /zh/proposal/upload_abstract/1/
Exception Value: 'ascii' codec can't encode characters in position 63-67: ordinal not in range(128)

主要原因似乎是 python 引用底層 os library 時,系統看不懂你使用的編碼(可是 python 懂 XD)。這時候你只要去系統安裝你需要的語言包。重新啟動 server 後問題就解決了。例如:

$ locale-gen zh_TW.UTF-8

參考資料

nginx - UnicodeEncodeError when uploading files in Django admin - Server Fault

← Python Wheel - 打包已編譯的 Python 套件 GMT v.s. Etc/GMT →
 
comments powered by Disqus