Apache: UnicodeEncodeError when opening Django template file with non-ascii character in file name

In my Django app I had a bit code that would check if a template with some file name exists:

from django.template.loader import get_template


def has_help_page(name):
    """Return whether a help page for the given name exists."""
    try:
        get_template(f"help/{name.lower()}.html")
    except TemplateDoesNotExist:
        return False
    return True

The name argument is derived from a Django model’s verbose name, and some models have verbose names that contain non-ascii characters (f.ex. “Broschüre”).

When running locally or in a Docker container (via mod_wsgi), this works absolutely fine. But when I let Apache serve the app from a Debian machine (Apache/2.4.56 (Debian)), and when I request a page that would call the above function with a name that contains an Umlaut I get an UnicodeEncodeError:

Traceback (most recent call last):
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/contrib/auth/mixins.py", line 109, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/views/generic/list.py", line 174, in get
    context = self.get_context_data()
  File "/srv/archiv/MIZDB/dbentry/site/views/base.py", line 1127, in get_context_data
    ctx = super().get_context_data(**kwargs)
  File "/srv/archiv/MIZDB/dbentry/search/mixins.py", line 62, in get_context_data
    ctx = super().get_context_data(**kwargs)  # type: ignore[misc]
  File "/srv/archiv/MIZDB/dbentry/site/views/base.py", line 783, in get_context_data
    ctx = super().get_context_data(**kwargs)
  File "/srv/archiv/MIZDB/dbentry/site/views/base.py", line 126, in get_context_data
    if has_help_page(self.opts.verbose_name.lower()):
  File "/srv/archiv/MIZDB/dbentry/site/views/help.py", line 15, in has_help_page
    get_template(f"help/{name.lower()}.html")
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/template/loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/template/backends/django.py", line 33, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/template/engine.py", line 175, in get_template
    template, origin = self.find_template(template_name)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/template/engine.py", line 157, in find_template
    template = loader.get_template(name, skip=skip)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/template/loaders/cached.py", line 57, in get_template
    template = super().get_template(template_name, skip)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/template/loaders/base.py", line 23, in get_template
    contents = self.get_contents(origin)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/template/loaders/cached.py", line 26, in get_contents
    return origin.loader.get_contents(origin)
  File "/srv/archiv/MIZDB/.venv/lib/python3.9/site-packages/django/template/loaders/filesystem.py", line 22, in get_contents
    with open(origin.name, encoding=self.engine.file_charset) as fp:

Exception Type: UnicodeEncodeError at /brochure/
Exception Value: 'ascii' codec can't encode character 'xfc' in position 39: ordinal not in range(128)

Where origin.name is:

'/srv/archiv/MIZDB/templates/help/broschüre.html'

Note that it crashes when trying to open the file, not when attempting to read it. The file, as such, also doesn’t exist; the name of the real, existing template does not contain non-ascii characters – it’s just the function making a guess with a probable template name based on a model.

I have since patched the function and got it all working again, but I am unsure what the real issue was here.

My questions are:

  1. Why is this only an issue when serving the app via Apache directly?
  2. Is there some kind of setting that I am missing? (I tried setting env vars and the default charset to no avail)
  3. Why is Apache trying to use “ascii” when the default and filesystem encoding is “utf-8”?

Apache site conf:

<Macro VHost $VENV_ROOT $PROJECT_ROOT>
        <VirtualHost *:80>
                ServerName localhost

                WSGIScriptAlias /miz $PROJECT_ROOT/MIZDB/wsgi.py
                WSGIDaemonProcess mizdb python-home=$VENV_ROOT python-path=$PROJECT_ROOT
                WSGIProcessGroup mizdb

                Alias /static $PROJECT_ROOT/static
                <Directory $PROJECT_ROOT/static>
                        Require all granted
                </Directory>

                <Directory $PROJECT_ROOT/MIZDB>
                        <Files wsgi.py>
                                Require all granted
                        </Files>
                </Directory>

        </VirtualHost>
</Macro>

USE VHost /srv/archiv/MIZDB/.venv /srv/archiv/MIZDB
UndefMacro VHost

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật