Search code examples
djangocharacter-encodingfilefield

Django FileField not working with german "Umlaut" (ä, ö, ü)


I use django, nginx and gunicorn.

In my models i have a FileField which is not working with german "Umlaute" in production.

When i try to upload a file (select file and push "save") in /admin with an "Umlaut":

UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 27: ordinal not in range(128)

models.py

class wiki(models.Model):

    headline = models.CharField(max_length=255)
    file = models.FileField(null=True, blank=True)

    def __str__(self):
        return self.headline

On the development Web server on the local machine (./manage runserver) it is working fine, so i thought it has something to do with gunicorn or nginx and did the following changes without success:

/etc/systemd/system/gunicorn.service:

(added --env LANG=de_DE.UTF-8)

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=django
Group=www-data
WorkingDirectory=/opt/django/project
ExecStart=/opt/django/bin/python3 /opt/django/bin/gunicorn --env LANG=de_DE.UTF-8 --workers 3 --bind unix:/opt/django/gunicorn.sock uhd.wsgi:application

[Install]
WantedBy=multi-user.target

/etc/nginx/sites-available/domain

server {
    charset UTF-8;
}

Here are some information which i think might be useful:

$ locale

LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=C.UTF-8

cat /etc/*-release

PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

i appreciate any help. If more informations are needed, please let me know.


Solution

  • I upgraded from Debian Jessie (oldstable) to Debian Stretch (stable) and now everything is working.

    Python was upgraded from Python 3.4.2 to Python 3.5.3. $ locale still gives the same output, gunicorn and nginx are still the same as shown in the question.

    $ cat /etc/*-release

    PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
    NAME="Debian GNU/Linux"
    VERSION_ID="9"
    VERSION="9 (stretch)"
    ID=debian
    HOME_URL="https://www.debian.org/"
    SUPPORT_URL="https://www.debian.org/support"
    BUG_REPORT_URL="https://bugs.debian.org/"