簡體   English   中英

Django:如何在save()上上傳沒有UnicodeEncodeError的文件?

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

IM嘗試上載文件,但是當文件名中的字符如“ó”具有“ ascii”編解碼器時,無法在位置59編碼字符u'\\ xf3':序數不在range(128)中,請嘗試obj.file = smart_unicode(form.cleaned_data["file"])在views.py上的save()之前的obj.file = smart_unicode(form.cleaned_data["file"])

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

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

在models.py上,但是使用view.py時沒有錯誤,在保存的模型上沒有正確的名稱文件,但是沒有文件,並且在不使用smart_unicode時有錯誤,沒有特殊的字體字體,上載有問題。

此網址包含有關Django的信息:如何在save()上上傳沒有UnicodeEncodeError的文件?

一個適合我的工作:

import locale
import sys

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM