Django创建APP时出现name ‘os‘ is not defined的错误的解决办法

安装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’,
],
},
},
]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值