2017-09-01 78 views
0

上傳日本命名的圖像時我收到的錯誤是:在45-48位置ASCII」編解碼器無法編碼的字符:在範圍內(128)Django的圖像上載不能上傳日本命名的圖像

序數不以英文字符命名時,圖片可以完美上傳。此外,奇怪的是,我遇到的錯誤只是當我上傳到服務器。日文命名不會在部署服務器上傳,但在開發過程中工作正常。

我的模型:

class Tenant_Post(models.Model): 
    user = models.ForeignKey(User,on_delete=models.CASCADE,null=True) 
    name = models.CharField(max_length=255,null=True) 
    image = models.FileField(upload_to='image/tenant/',null=True) 
    posted_on = models.DateTimeField(auto_now_add=True, auto_now=False) 
    last_modified_on = models.DateTimeField(auto_now_add=False, 
    auto_now=True) 

    def __unicode__(self): 
     return self.name 

我的觀點:

@login_required(login_url='/') 
def new(request): 
    if request.method == 'POST': 
     print request.POST 
     form = TenantForm(request.POST or None, request.FILES or None) 
     if form.is_valid(): 
      instance = form.save(commit=False) 
      instance.user = request.user 
      instance.save() 
      print 'success' 
      return HttpResponseRedirect(reverse('tenant:all')) 
     else: 
      print 'fail' 
      return render(request,'tenant/new.html',{'form':form,}) 
    else: 
     form = TenantForm() 
     return render(request,'tenant/new.html',{'form':form,}) 

完全溯源是在這裏:

File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner 
    41.    response = get_response(request) 

File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 
    187.     response = self.process_exception_by_middleware(e, request) 

File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 
    185.     response = wrapped_callback(request, *callback_args, **callback_kwargs) 

File "/opt/python/run/venv/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 
    23.     return view_func(request, *args, **kwargs) 

File "/opt/python/current/app/tenant/views.py" in edit 
    64.     instance.save() 

File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/models/base.py" in save 
    806.      force_update=force_update, update_fields=update_fields) 

File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/models/base.py" in save_base 
    836.    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) 

File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/models/base.py" in _save_table 
    900.      for f in non_pks] 

File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/models/fields/files.py" in pre_save 
    296.    file.save(file.name, file.file, save=False) 

File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/models/fields/files.py" in save 
    94.   self.name = self.storage.save(name, content, max_length=self.field.max_length) 

File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/files/storage.py" in save 
    53.   name = self.get_available_name(name, max_length=max_length) 

File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/files/storage.py" in get_available_name 
    77.   while self.exists(name) or (max_length and len(name) > max_length): 

File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/files/storage.py" in exists 
    392.   return os.path.exists(self.path(name)) 

File "/opt/python/run/baselinenv/lib64/python2.7/genericpath.py" in exists 
    26.   os.stat(path) 

Exception Type: UnicodeEncodeError at /tenant/edit/4/ 
Exception Value: 'ascii' codec can't encode characters in position 45-48: ordinal not in range(128) 
+0

ascii不能保存japanise名稱,嘗試重命名或更改編碼 – Vladyslav

+0

我不允許更改名稱,可以告訴我更多關於更改編碼的信息嗎? – jencko

+0

https://stackoverflow.com/questions/36179539/encode-base64-django-imagefield-stream – Vladyslav

回答

1

我還沒有與日本語言測試,但它與其他一些語言就像葡萄牙的特殊人物:

添加這對您的settings.py

DEFAULT_FILE_STORAGE = 'app.models.ASCIIFileSystemStorage' 

而且你app.models.ASCIIFileSystemStorage

# This Python file uses the following encoding: utf-8 
from django.db import models 
from django.core.files.storage import FileSystemStorage 
import unicodedata 

class ASCIIFileSystemStorage(FileSystemStorage): 
    """ 
    Convert unicode characters in name to ASCII characters. 
    """ 
    def get_valid_name(self, name): 
     name = unicodedata.normalize('NFKD', name).encode('ascii', 'ignore') 
     return super(ASCIIFileSystemStorage, self).get_valid_name(name) 
+0

它工作的感謝! – jencko

-1

加編碼UTF-8則可以接受大多數語言 也到模板html使用字符集utf-8

# -*- coding: UTF-8 -*- in at the top the file 

<meta charset="utf-8"/> in html templates 

編輯: 請閱讀官方文檔或添加此行

from __future__ import unicode_literals 

https://docs.djangoproject.com/en/1.11/ref/unicode/

+0

抱歉不工作! – jencko

+0

第一個將只定義該模塊中文字字符串的編碼,第二個將告訴瀏覽器將內容解釋爲utf-8編碼,這在這裏完全不相關(不會修復系統編碼),甚至有害如果你的內容不是utf-8編碼。另外Django知道如何通過適當的響應頭指定有效的編碼(在項目的設置文件中使用的編碼),所以它在django項目中完全沒用。 –

1

你的錯誤是由os.stat(path)通話,這意味着你的文件系統不支持日文字符(實際上它可能只支持ASCII或一些拉丁-XXX或凸起windows-yyy編碼)。

這裏主要有兩種解決方法:要麼將系統配置爲在任何地方使用utf-8(無論如何IMVHO都是一個理智的事情),要麼確保只對文件系統使用系統編碼(或者簡單的ascii)名字等(參見leeeandroo的回答)。

+0

它工作正常,但我仍然想知道我如何配置我的系統到處使用utf-8。我在部署在我的AWS EC2而不是在我的開發環境中時遇到了這個問題。 – jencko

+0

我不能幫助AWS,抱歉。 –