Django makemessages,类型错误:预期的 str、字节或 os.PathLike 对象,而不是列表

问题描述 投票:0回答:1

我正在尝试在 pycharm 中创建一个多语言 Django 项目。但一整天都在努力弄清楚为什么 makemessages 不生成 .po 文件。所以我寻求一些帮助。

设置.py

from pathlib import Path
import os
from decouple import config
from unipath import Path
from django.utils.translation import gettext_lazy as _
import environ

env = environ.Env()
environ.Env.read_env()


BASE_DIR = Path(__file__).resolve().parent.parent
CORE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale/'),
)
LANGUAGE_CODE = 'en-us'

LANGUAGES = [
    ('en', _('English')),
    ('fr', _('French')),
]

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

当我运行 python manage.py makemessages -l fr 时,我得到以下响应:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_manage.py", line 52, in <module>
    run_command()
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_manage.py", line 46, in run_command
    run_module(manage_file, None, '__main__', True)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 225, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/Users/famronnebratt/PycharmProjects/CompValid3/manage.py", line 22, in <module>
    main()
  File "/Users/famronnebratt/PycharmProjects/CompValid3/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/commands/makemessages.py", line 430, in handle
    potfiles = self.build_potfiles()
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/commands/makemessages.py", line 506, in build_potfiles
    file_list = self.find_files(".")
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/commands/makemessages.py", line 543, in find_files
    ignored_roots = [
  File "/Users/famronnebratt/PycharmProjects/CompValid3/venv/lib/python3.9/site-packages/django/core/management/commands/makemessages.py", line 544, in <listcomp>
    os.path.normpath(p)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/posixpath.py", line 336, in normpath
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not list

Process finished with exit code 1
django pycharm multilingual
1个回答
0
投票

原因不是

LOCALE_PATHS
。 Django 会忽略
STATIC_ROOT
 [Django-doc]
MEDIA_ROOT
 [Django-doc]
。确实:

def find_files(self, root):
    """
    Get all files in the given root. Also check that there is a matching
    locale dir for each file.
    """
    all_files = []
    ignored_roots = []
    if self.settings_available:
        ignored_roots = [
            os.path.normpath(p)
            for p in (settings.MEDIA_ROOT, settings.STATIC_ROOT)
            if p
        ]
    # …

这些显然是您的

settings.py
中的(非空)列表。

因此您应该重写这些设置,因此:

# settings.py

# …

STATIC_ROOT = [my_path]

应重写为:

# settings.py

# …

STATIC_ROOT = my_path

MEDIA_ROOT
相同。

© www.soinside.com 2019 - 2024. All rights reserved.