【问题标题】:Django TypeError at admin: expected str, bytes or os.PathLike object, not PosixPath管理员处的 Django TypeError:预期的 str、字节或 os.PathLike 对象,而不是 PosixPath
【发布时间】:2021-10-09 09:38:24
【问题描述】:

目前我刚刚将我的 django 项目移到了一个新的 ubuntu 服务器上。但是当我尝试访问管理网站时,它总是返回“TypeError at /admin/: expected str, bytes or os.PathLike object, not PosixPath”。我之前服务器的环境是:Python3.6.9和django 3.0.4。当前版本是 Python 3.8 和 Django 3.2.5。版本更新后有变化吗?

settings.py 是:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'XXXX'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'MyBlog',
    ...

]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


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',
                'MyBlog.context_processor.Alert_stat',
            ],
        },
    },
]

WSGI_APPLICATION = 'MyBlog.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'myweb',
        'USER':'root',
        'PASSWORD':'112321',
        'HOST':'localhost',
        'PORT':'3306',
        'OPTIONS':{'charset':'utf8mb4'},
    }
}

...

STATIC_URL = '/static/'
STATICFILES_DIRS = [(
    os.path.join(BASE_DIR,'static')
)]

#templates dirs
TEMPLATES_DIRS = (os.path.join(BASE_DIR, 'templates'))

#Media dirs

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

【问题讨论】:

    标签: python django


    【解决方案1】:

    我注意到错误是由 Django 3.2.5 和 Django 3.0.4 之间的版本差异引起的。主要问题来自settints.py 中的路径设置。如果有人遇到类似问题,这些问答可能会有所帮助:

    Django TypeError: argument of type 'PosixPath' is not iterable

    "TypeError at / argument 1 must be str, not PosixPath" in django ecommerce website

    TypeError: expected str, bytes or os.PathLike object, not tuple in Django

    我的解决方案是将 django 降级到 3.0.4。

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 2017-10-23
      • 2020-07-12
      • 2019-04-11
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多