我启动一个用pycharm刚刚创建好的django项目 报错如下:D:\django2>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 22, in
execute_from_command_line(sys.argv)
File "D:\Python36\lib\site-packages\django\core\management_init_.py", line 364, in execute_from_command_line
utility.execute()
File "D:\Python36\lib\site-packages\django\core\management_init_.py", line 308, in execute
settings.INSTALLED_APPS
File "D:\Python36\lib\site-packages\django\conf_init_.py", line 56, in getattr
self.setup(name)
File "D:\Python36\lib\site-packages\django\conf_init.py", line 41, in setup
self.wrapped = Settings(settings_module)
File "D:\Python36\lib\site-packages\django\conf_init.py", line 110, in init
mod = importlib.import_module(self.SETTINGS_MODULE)
File "D:\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 "D:\django2\django2\settings.py", line 57, in
'DIRS': [BASE_DIR / 'templates']
TypeError: unsupported operand type(s) for /: 'str' and 'str'
execute_from_command_line(sys.argv)
File "D:\Python36\lib\site-packages\django\core\management_init_.py", line 364, in execute_from_command_line
utility.execute()
File "D:\Python36\lib\site-packages\django\core\management_init_.py", line 308, in execute
settings.INSTALLED_APPS
File "D:\Python36\lib\site-packages\django\conf_init_.py", line 56, in getattr
self.setup(name)
File "D:\Python36\lib\site-packages\django\conf_init.py", line 41, in setup
self.wrapped = Settings(settings_module)
File "D:\Python36\lib\site-packages\django\conf_init.py", line 110, in init
mod = importlib.import_module(self.SETTINGS_MODULE)
File "D:\Python36\lib\importlib_init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "
File "
File "
File "
File "
File "
File "D:\django2\django2\settings.py", line 57, in
'DIRS': [BASE_DIR / 'templates']
TypeError: unsupported operand type(s) for /: 'str' and 'str'
这个错误是由于在Django的settings.py文件中,你尝试使用Python 3.6中不存在的路径操作方式(/)来合并目录路径。在Python 3.6中,字符串(str)对象并不支持使用/来直接进行路径拼接,这是从Python 3.8开始引入的pathlib模块中Path对象的一个特性。
为了解决这个问题,你可以使用os.path.join来替代/进行路径的拼接。下面是如何修改你的settings.py文件的示例:
首先,确保你已经导入了os模块:
python
import os
然后,修改你的TEMPLATES设置中的DIRS部分,使用os.path.join来拼接路径:
python
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))
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',
],
},
},
]
这样修改后,你的Django项目就应该能够正确运行,不再出现TypeError: unsupported operand type(s) for /: 'str' and 'str'的错误了。
如果你想要使用Python 3.8及以上版本的路径操作特性,你可以考虑升级你的Python环境。但如果你目前受限于必须使用Python 3.6,那么使用os.path.join是最简单且兼容的方法。
标签:启动,py,django,init,File,pycharm,line,os From: https://www.cnblogs.com/manbo112358/p/18312486