Search code examples
pythondjangofile-uploadutf-8tastypie

how can save file with names in utf-8


I need to save file with utf-8 names.but when I do it django error:

UnicodeEncodeError at /uploaded/document/ 'فیلتر.png'
'ascii' codec can't encode characters in position 55-59: ordinal not in range(128)

although, my filefield like it:

# -*- coding: utf-8 -*-
def get_path(instance, filename):
    return u' '.join((u'document', filename)).encode('utf-8').strip()

class Document(models.Model):
    file_path = models.FileField(verbose_name='File', upload_to=get_path,
                                 storage=FileSystemStorage(base_url=settings.LOCAL_MEDIA_URL))

how can I fix it? I use tastypie api to upload file.


Solution

  • my question answered here: https://itekblog.com/ascii-codec-cant-encode-characters-in-position/#The_Code

    I should change apache2 encoding: /etc/apache/envvars

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