UnicodeDecodeError with Django's request.FILES

后端 未结 4 1548
轻奢々
轻奢々 2021-02-09 04:41

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:05

    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'
    

提交回复
热议问题