上傳日本命名的圖像時我收到的錯誤是:在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)
ascii不能保存japanise名稱,嘗試重命名或更改編碼 – Vladyslav
我不允許更改名稱,可以告訴我更多關於更改編碼的信息嗎? – jencko
https://stackoverflow.com/questions/36179539/encode-base64-django-imagefield-stream – Vladyslav