settings.py,TypeError:不支持的操作数类型用于/:'str'和'str'。

8

在进行迁移时,我遇到了这个错误信息:

line 82, in <module>
    'NAME': BASE_DIR / 'db.sqlite3',
TypeError: unsupported operand type(s) for /: 'str' and 'str'

第四行是这里的第82行:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

这里是settings.py文件的所有代码(我去掉了密钥):
"""
Django settings for ProTwo project.

Generated by 'django-admin startproject' using Django 3.1.1.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

import os
from pathlib import Path

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


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

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

# 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',
    'appTwo'
]

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',
]

ROOT_URLCONF = 'ProTwo.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR,],
        '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'ProTwo.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'

有什么想法吗?我不明白的是,我从未接触过第82行,那是文件中原有的代码。我添加的代码在顶部:

import os
from pathlib import Path

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

然后在TEMPLATES中,我添加了:

'DIRS': [TEMPLATE_DIR,],

最后在 INSTALLED_APPS 中,我添加了:

'appTwo'

谢谢

8个回答

11
你正在尝试用字符串除以字符串。 要连接路径的两个部分,请按照以下步骤操作:
os.path.join(BASE_DIR, 'db.sqlite3')

我应该把它放在顶部的 # Build paths inside the project like this: BASE_DIR / 'subdir' 下面吗?我已经把它放在那里了,但仍然收到相同的错误信息:" line 85, in <module> 'NAME': BASE_DIR / 'db.sqlite3', TypeError: unsupported operand type(s) for /: 'str' and 'str'" - JohnHarbaugh
@go2nirvana 能回答这个问题吗: https://stackoverflow.com/questions/65213757/how-to-set-base-dir-for-the-production-settings-in-django-3-1-4 - Aakash Solanki

5

将第80行替换为以下内容:

'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

它在我的项目上有效,希望对你也有用。


3

我认为你需要重写你的BASE_DIR代码。你的代码应该是:

BASE_DIR = Path(__file__).resolve().parent.parent

你能回答这个问题吗: https://stackoverflow.com/questions/65213757/how-to-set-base-dir-for-the-production-settings-in-django-3-1-4 - Aakash Solanki

0

请将您的STATIC_ROOT替换为以下内容

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

你的回答与主题无关。问题提出者并没有询问STATIC_ROOT - woodz

0

#我通过更改settings.py中的BASE_DIR解决了这个错误

import os
from pathlib import Path

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

0
DB_PATH = Path(__file__).resolve().parent.parent.parent.parent

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': DB_PATH / 'db/db.sqlite3',
}

}

我的文件目录是home/user/db,所以我做的是,移动到Django项目当前目录的父级父级并将'db/'与'db.sqlite3'连接起来。它有效了。


0

0

谢谢go2nirvanna,以及另一篇文章中说:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR , 'db.sqlite3'),
    }
}

完成了!

这是文章链接: 使用Django创建SQLite 3数据库


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接