我正在学习 Djnago,我正面临一个我环顾四周但无法解决的问题。 所以基本上在 Django For Beginners 的第 45 页上,作者说:
" 接下来我们需要更新 config/settings.py 来告诉 Django 我们新模板的位置 目录。这是对 TEMPLATES 下设置 'DIRS' 的一行更改。” 代码
TEMPLATES = [
{
...
'DIRS': [str (BASE_DIR. joinpath('templates '))], # new
...
},
]
所以在我的 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',
],
},
},
]
不幸的是,当我运行 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'
我敢打赌这个错误是非常基本的,我没有朝着正确的方向寻找,但是自从我开始寻找答案已经快一天了,所以如果有人能帮助我,我会非常感激。
谢谢大家,祝您有美好的一天。
答案 0 :(得分:1)
首先,您需要在 settings.py 上“导入 os”。
然后转到 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',
],
},
},
]
#我希望你能得到你的解决方案。谢谢。
答案 1 :(得分:-1)
加入就是你可以使用`
<块引用>'DIRS': [str(os.path.join(BASE_DIR,"Templates"))],
谢谢