I want to handle the file uploaded by the guest, here is the views code:
def charset(request):
logging.info('charset')
name = request.GET['name']
file_path = os.path.join(settings.MEDIA_ROOT, name)
logging.info(file_path)
logging.info(type(file_path))
file1 = open(file_path.decode('utf8'), 'wb')
file1.write(b'test')
file1.close()
return HttpResponse('success')
But an error occurred. The trace back is following:
UnicodeEncodeError at /upload/charset/
'ascii' codec can't encode characters in position 27-28: ordinal not in range(128)
You can repeat the error by visiting: My Wrong Website
I'm using environment as these:
python 3.4.3
django 1.9.3
apache2 2.4.7
But when I run server using python3 manage.py runserver 0.0.0.0:8000. It works.
How can I fix this? Thank you for your attention. You can view the project code on the website github_of_my_django_tutorial.
decodecall.use encode("utf8")to solve this.