Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeEncodeError when uploading non-ascii filenames #69

Closed
Chrescht opened this issue Jun 11, 2021 · 3 comments
Closed

UnicodeEncodeError when uploading non-ascii filenames #69

Chrescht opened this issue Jun 11, 2021 · 3 comments

Comments

@Chrescht
Copy link

When uploading a file with non-ascii characters, I get the following error:

UnicodeEncodeError at /url_removed/

'ascii' codec can't encode character '\xe9' in position 56: ordinal not in range(128)

Request Method: 	POST
Request URL: 	url_removed
Django Version: 	3.2
Exception Type: 	UnicodeEncodeError
Exception Value: 	

'ascii' codec can't encode character '\xe9' in position 56: ordinal not in range(128)

These are the steps to reproduce the issue:

  1. PrivateFleField in my model: id_file = PrivateFileField(upload_to='id/', max_file_size=size_3mb, null=True, blank=True) and add it to a form.
  2. upload a PDF file with filename containing the character é as an example: "carte d'identité.pdf"
  3. Django throws an UnicodeEncodeError

Where can I set the encoding codec?

Python 3.8.5 and Django 3.2

@Chrescht
Copy link
Author

Chrescht commented Jun 16, 2021

Does nobody have an idea how to change the default ascii encoding?

The Python default encoding is utf-8:
python -c 'import sys; print(sys.getdefaultencoding())'

@Chrescht
Copy link
Author

Chrescht commented Jun 16, 2021

At least several of the main developers should know exactly how to deal with this.

@Chrescht
Copy link
Author

Chrescht commented Jun 23, 2021

The issue was with the apache webserver after all.
Adding the following two lines to the file etc/apache2/envvars did the trick:

export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

Don't forget to restart apache. A reload is not sufficient.
source: https://code.djangoproject.com/wiki/django_apache_and_mod_wsgi#AdditionalTweaking

Displaying the file does not work yet though..the non-ascii file is not found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant