【问题标题】:Django, Docker, collectstatic, UnicodeEncodeError: 'utf-8' codec can't encode characters in position 0-1: surrogates not allowedDjango, Docker, collectstatic, UnicodeEncodeError: 'utf-8' codec can't encode characters in position 0-1: surrogates not allowed
【发布时间】:2020-05-22 17:28:02
【问题描述】:

我希望你能在这里帮助我。在 Django 3.0.6 中运行“collectstatic”时出现 UnicodeEncodeError。我之前通过将语言环境变量 LANG 和 LC_ALL 设置为 UTF-8 修复了一个类似的错误,但在这种情况下这似乎不起作用。

我的 Django 项目在容器中运行(Docker 版本 18.03.1-ce),我刚刚升级到 Django 3.0.6。此外,在此过程中,从 Python 2.7 移至 Python 3.6。容器是“FROM python:3.6”构建的。除了collectstatic,一切正常。静态文件存储在 S3 存储桶中,并且在升级之前一切正常(由于 Python 升级,我不得不修复一些 Django 问题,但所有这些都是可预测和记录的)。

容器中的区域设置(带有错误消息)是:

    root# locale
    locale: Cannot set LC_CTYPE to default locale: No such file or directory
    locale: Cannot set LC_MESSAGES to default locale: No such file or directory
    locale: Cannot set LC_ALL to default locale: No such file or directory
    LANG=en_US.UTF-8
    LANGUAGE=en_US:en
    LC_CTYPE="en_US.UTF-8"
    LC_NUMERIC="en_US.UTF-8"
    LC_TIME="en_US.UTF-8"
    LC_COLLATE="en_US.UTF-8"
    LC_MONETARY="en_US.UTF-8"
    LC_MESSAGES="en_US.UTF-8"
    LC_PAPER="en_US.UTF-8"
    LC_NAME="en_US.UTF-8"
    LC_ADDRESS="en_US.UTF-8"
    LC_TELEPHONE="en_US.UTF-8"
    LC_MEASUREMENT="en_US.UTF-8"
    LC_IDENTIFICATION="en_US.UTF-8"
    LC_ALL=en_US.UTF-8

以及运行 collectstatic 时的消息(来自容器内):

    root# python manage.py collectstatic

    You have requested to collect static files at the destination
    location as specified in your settings.

    This will overwrite existing files!
    Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: yes
    Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in                                 execute_from_command_line
        utility.execute()
      File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in         execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 328, in         run_from_argv
        self.execute(*args, **cmd_options)
      File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 369, in         execute
        output = self.handle(*args, **options)
      File "/usr/local/lib/python3.6/site-        packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 187, in handle
        collected = self.collect()
      File "/usr/local/lib/python3.6/site-        packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 113, in collect
        handler(path, prefixed_path, storage)
      File "/usr/local/lib/python3.6/site-        packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 338, in copy_file
        if not self.delete_file(path, prefixed_path, source_storage):
      File "/usr/local/lib/python3.6/site-        packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 248, in delete_file
        if self.storage.exists(prefixed_path):
      File "/usr/local/lib/python3.6/site-packages/storages/backends/s3boto.py", line 464, in exists
        return self._get_key(name) is not None
      File "/usr/local/lib/python3.6/site-packages/storages/backends/s3boto.py", line 450, in _get_key
        return self.bucket.get_key(self._encode_name(name))
      File "/usr/local/lib/python3.6/site-packages/boto/s3/bucket.py", line 193, in get_key
        key, resp = self._get_key_internal(key_name, headers, query_args_l)
      File "/usr/local/lib/python3.6/site-packages/boto/s3/bucket.py", line 200, in _get_key_internal
        query_args=query_args)
      File "/usr/local/lib/python3.6/site-packages/boto/s3/connection.py", line 654, in make_request
        path = self.calling_format.build_path_base(bucket, key)
      File "/usr/local/lib/python3.6/site-packages/boto/s3/connection.py", line 98, in         build_path_base
        key = boto.utils.get_utf8_value(key)
      File "/usr/local/lib/python3.6/site-packages/boto/utils.py", line 863, in get_utf8_value
        value = value.encode('utf-8')
    UnicodeEncodeError: 'utf-8' codec can't encode characters in position 0-1: surrogates not allowed

Dockerfile sn-p:

    #FROM ubuntu:14.04
    FROM python:3.6

    MAINTAINER Paul Young
    # Set the locale

    #RUN apt-get -y install locales
    #RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    #    locale-gen
    ENV LANG en_US.UTF-8
    ENV LANGUAGE en_US:en
    ENV LC_ALL en_US.UTF-8

    RUN apt-get update
    RUN apt-get install -y python python-pip python-dev
    RUN apt-get install -y libxml2-dev libxslt-dev libffi-dev libssl-dev
    #RUN apt-get install -y libmysqlclient-dev
    RUN apt-get install -y default-libmysqlclient-dev

而且我很确定我可能错过了一些非常明显的东西!谢谢...

【问题讨论】:

    标签: django python-3.x docker


    【解决方案1】:

    我从 Dockerfile 中注释掉以下内容并重新构建了容器,出于某种原因,这解决了问题:

        #ENV LANG en_US.UTF-8
        #ENV LANGUAGE en_US:en
        #ENV LC_ALL en_US.UTF-8
    

    【讨论】:

      猜你喜欢
      • 2015-02-06
      • 1970-01-01
      • 2021-08-15
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-26
      • 2020-04-19
      相关资源
      最近更新 更多