UnicodeDecodeError with Django's request.FILES

后端 未结 4 1784
自闭症患者
自闭症患者 2021-02-09 04:28

I have the following code in the view call..

def view(request):
    body = u\"\"  
    for filename, f in request.FILES.items():
        body = body + \'Filename         


        
4条回答
  •  礼貌的吻别
    2021-02-09 05:08

    Django has some utilities that handle this (smart_unicode, force_unicode, smart_str). Generally you just need smart_unicode.

    from django.utils.encoding import smart_unicode
    def view(request):
        body = u""  
        for filename, f in request.FILES.items():
            body = body + 'Filename: ' + filename + '\n' + smart_unicode(f.read()) + '\n'
    

提交回复
热议问题