安装Django环境后,在执行python manage.py startapp index后出现如下错误
***E:codeDjangoP1>python manage.py startapp index Traceback (most recent call last): File "manage.py", line 22, inmain() File "manage.py", line 18, in main execute_from_command_line(sys.argv) File "E:Python38libsite-packagesdjangocoremanagement__init__.py", line 419, in execute_from_command_line utility.execute() File "E:Python38libsite-packagesdjangocoremanagement__init__.py", line 363, in execute settings.INSTALLED_APPS File "E:Python38libsite-packagesdjangoconf__init__.py", line 82, in __getattr__ self._setup(name) File "E:Python38libsite-packagesdjangoconf__init__.py", line 69, in _setup self._wrapped = Settings(settings_module) File "E:Python38libsite-packagesdjangoconf__init__.py", line 170, in __init__ mod = importlib.import_module(self.SETTINGS_MODULE) File "E:Python38libimportlib__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File " ", line 1014, in _gcd_import File " ", line 991, in _find_and_load File " ", line 975, in _find_and_load_unlocked File " ", line 671, in _load_unlocked File " ", line 783, in exec_module File " ", line 219, in _call_with_frames_removed File "E:codeDjangoP1DjangoP1settings.py", line 57, in '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’,
],
},
},
]



