安装Django环境后,在执行python manage.py startapp index后出现如下错误
***E:\code\DjangoP1>python manage.py startapp index Traceback (most recent call last): File "manage.py", line 22, in <module>
main() File "manage.py", line 18, in main
execute_from_command_line(sys.argv) File "E:\Python38\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute() File "E:\Python38\lib\site-packages\django\core\management\__init__.py", line 363, in execute
settings.INSTALLED_APPS File "E:\Python38\lib\site-packages\django\conf\__init__.py", line 82, in
__getattr__
self._setup(name) File "E:\Python38\lib\site-packages\django\conf\__init__.py", line 69, in
_setup
self._wrapped = Settings(settings_module) File "E:\Python38\lib\site-packages\django\conf\__init__.py", line 170, in
__init__
mod = importlib.import_module(self.SETTINGS_MODULE) File "E:\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 783, in exec_module File "<frozen importlib._bootstrap>", line 219, in
_call_with_frames_removed File "E:\code\DjangoP1\DjangoP1\settings.py", line 57, in <module> 'DIRS':
[os.path.join(BASE_DIR, 'templates')] NameError: name 'os' is not defined***
根据内容分析,时程序在执行settings.py 57行时,对出现的os.path.join(BASE_DIR, ‘templates’)未加以引用。
因此需要在settings.py前加入
import os
该问题仅在pycharm自动创建的脚本中出现
因为pycharm在自动创建templates文件夹时,在setting.py的路径信息使用了os模块,但Djiango3.1后,路径信息采用pathlib模块,所以会导致该问题的出现。下面是Djiango3.1后的路径信息。
TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
‘DIRS’: [],
‘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’,
],
},
},
]