Search code examples
pythondjangofile-uploadlocalepython-unicode

Django: How to upload file without UnicodeEncodeError on save()?


IM try to upload a file but when the file name have caracters like "ó" have a 'ascii' codec can't encode character u'\xf3' in position 59: ordinal not in range(128) im try to obj.file = smart_unicode(form.cleaned_data["file"]) before save() on views.py

and

def file(instance, filename):
    i = datetime.now()
    filename = smart_unicode(filename)

    return "archivos/files/%s_%s" % (str("a")+i.strftime('%Y-%m-%d'), filename)

on models.py, but when using view.py dont have error and on saved model have the name file correct but dont have file, and when not using smart_unicode have error, with out especial caracter font have problem to upload.


Solution

  • this url have information about, Django: How to upload file without UnicodeEncodeError on save()?

    an for me work this:

    import locale
    import sys
    
    def my_views(request):
        # before 
        reload(sys)
        sys.setdefaultencoding("utf-8")