进行操作python3 manage.py runserver或者python3 manage.py migrate等一系列的围绕manage.py的操作时,可能会出现下面的错误。
root@iZ2ze69ip1k885e39qa56cZ:/var/www/djangoProject11# python3 manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f3438178
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", line 225, in wrappe
fn(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/runserver.py", l
self.check(display_num_errors=True)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 379, in ch
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 366, in _r
return checks.run_checks(**kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/core/checks/registry.py", line 71, in run
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/staticfiles/checks.py", line 9, i
finder_errors = finder.check()
File "/usr/local/lib/python3.5/dist-packages/django/contrib/staticfiles/finders.py", line 82,
if settings.STATIC_ROOT and os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root):
File "/usr/lib/python3.5/posixpath.py", line 357, in abspath
if not isabs(path):
File "/usr/lib/python3.5/posixpath.py", line 64, in isabs
return s.startswith(sep)
AttributeError: 'PosixPath' object has no attribute 'startswith'
运行代码前,我的setting.py代码为:
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
......
......
STATIC_URL = ‘/static/’
STATIC_ROOT=BASE_DIR.joinpath(‘static’)
STATICFILES_DIRS = (
BASE_DIR.joinpath(‘static’),
)
出现这种错误是因为你使用的是python3.5,这个版本的os模块还没有支持Path对象。
方法一:可以将python3.5升级为python3.6或者以上版本,并将其使用(具体自己去搜就行了,网上一大堆。)。
方法二:既然python3.5不支持Path对象,那我就不使用它(上面的代码框里面的
BASE_DIR = Path(__file__).resolve().parent.parent
使用了Path),
可以使用代码BASE_DIR = os.path.abspath(os.path.join(__file__, '../../../'))
对其进行替代。
替代后的代码为:
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = os.path.abspath(os.path.join(__file__, '../../../'))
......
......
STATIC_URL = ‘/static/’
STATIC_ROOT=BASE_DIR.joinpath(‘/static/’)
STATICFILES_DIRS = (
BASE_DIR.joinpath(‘static’),
)
问题解决了。