简体   繁体   English

Django 初学者 - 运行 manage.py runserver 时出现 Joinpath 错误

[英]Django For Beginers - Joinpath error when running manage.py runserver

I am learning Djnago and I am facing an issue I have looked around but can't maange to solve it.我正在学习 Djnago,我遇到了一个我环顾四周但无法解决的问题。 So basically on page 45 of Django For Beginners the author says:所以基本上在 Django For Beginners 的第 45 页上,作者说:

" Next we need to update config/settings.py to tell Django the location of our new templates directory. This is a one-line change to the setting 'DIRS' under TEMPLATES." “接下来我们需要更新 config/settings.py 来告诉 Django 我们新模板目录的位置。这是对 TEMPLATES 下的设置 'DIRS' 的单行更改。” Code代码

config/settings.py配置/设置.py

TEMPLATES = [
{
...
'DIRS': [str (BASE_DIR. joinpath('templates '))], # new
...
},
]

So in my settings.py file I have this:所以在我的 settings.py 文件中我有这个:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [str(BASE_DIR.joinpath('templates'))],  # new
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Unfortunatelly, when I run the manage.py runserver I then get this message:不幸的是,当我运行 manage.py runserver 时,我会收到以下消息:

  File "C:\Users\User\Desktop\pages\manage.py", line 22, in <module>
    main()
  File "C:\Users\User\Desktop\pages\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\User\.virtualenvs\pages-8ZErAoFj\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\User\.virtualenvs\pages-8ZErAoFj\lib\site-packages\django\core\management\__init__.py", line 363, in execute
    settings.INSTALLED_APPS
  File "C:\Users\User\.virtualenvs\pages-8ZErAoFj\lib\site-packages\django\conf\__init__.py", line 82, in __getattr__
    self._setup(name)
  File "C:\Users\User\.virtualenvs\pages-8ZErAoFj\lib\site-packages\django\conf\__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\User\.virtualenvs\pages-8ZErAoFj\lib\site-packages\django\conf\__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "c:\users\user\appdata\local\programs\python\python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\User\Desktop\pages\config\settings.py", line 58, in <module>
    'DIRS': [str(BASE_DIR.joinpath('templates'))],  # new
AttributeError: 'str' object has no attribute 'joinpath'

I bet the error is very basic and I am not looking in the right direction but it's been nearly a day since I have started searching for an answer so if someone could help me I would be really gratefull.我敢打赌,这个错误是非常基本的,我没有朝着正确的方向寻找,但是自从我开始寻找答案以来已经将近一天了,所以如果有人可以帮助我,我将非常感激。

Thanks all and have a great day.谢谢大家,祝你有美好的一天。

First, you need to "import os" on settings.py.首先,您需要在 settings.py 上“导入操作系统”。

导入操作系统

Then go to TEMPLATES and change the...然后 go 到 TEMPLATES 并更改...

'DIRS': [os.path.join(BASE_DIR, 'templates'),],

'DIRS': [os.path.join(BASE_DIR, 'templates'),],

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates'),],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},

] ]

#I hope you get your solution. #我希望你能得到你的解决方案。 Thank You.谢谢你。

join that is u can use `加入即你可以使用`

'DIRS': [str(os.path.join(BASE_DIR,"Templates"))], 'DIRS': [str(os.path.join(BASE_DIR,"模板"))],

thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM