Django启动服务出现:NameError: name 'os' is not defined

环境信息:

python3.6
Django:Django2.1.4

在第一次启动Django服务的时候出现错误:

D:\JetBrains\PyCharm\bin\runnerw64.exe C:\Users\Administrator\PycharmProjects\untitled2\venv\Scripts\python3.exe C:/Users/Administrator/PycharmProjects/untitled2/manage.py runserver 127.0.0.1:8000 runserver 0.0.0.0:8000
Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/untitled2/manage.py", line 22, in 
    main()
  File "C:/Users/Administrator/PycharmProjects/untitled2/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Administrator\PycharmProjects\untitled2\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\Administrator\PycharmProjects\untitled2\venv\lib\site-packages\django\core\management\__init__.py", line 363, in execute
    settings.INSTALLED_APPS
  File "C:\Users\Administrator\PycharmProjects\untitled2\venv\lib\site-packages\django\conf\__init__.py", line 82, in __getattr__
    self._setup(name)
  File "C:\Users\Administrator\PycharmProjects\untitled2\venv\lib\site-packages\django\conf\__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\Administrator\PycharmProjects\untitled2\venv\lib\site-packages\django\conf\__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 994, in _gcd_import
  File "", line 971, in _find_and_load
  File "", line 955, in _find_and_load_unlocked
  File "", line 665, in _load_unlocked
  File "", line 678, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "C:\Users\Administrator\PycharmProjects\untitled2\untitled2\settings.py", line 57, in 
    'DIRS': [os.path.join(BASE_DIR, 'templates')]
NameError: name 'os' is not defined

出错原因:在项目下settings.py文件中“TEMPLATES”中,默认会增加一行“‘DIRS’: [os.path.join(BASE_DIR, ‘templates’)]”用于配置模板地址。
NameError: name ‘os’ is not defined 说明没有找到OS

解决方法:

1、在settings.py文件头部增导入OS
import os
2、不用增加导入,修改默认生成的“‘DIRS’: [os.path.join(BASE_DIR, ‘templates’)]”,修改如下:
‘DIRS’: [Path(BASE_DIR, ‘templates’)]
原因说明:
3.4版本之后,建议pathlib代替os

你可能感兴趣的:(Django启动服务出现:NameError: name 'os' is not defined)