return database_name == ':memory:' or 'mode=memory' in database_name TypeError: 'PosixPath' 类型的参数不可迭代

Nit*_*ini 6 python django django-deployment

我正在用 django 制作一个 webapp。我尝试将它部署在 pythonanywhere 上,一切正常,但最后当我尝试使用python manage.py collectstatic它收集静态文件时,出现了以下错误:

return database_name == ':memory:' or 'mode=memory' in database_name
TypeError: argument of type 'PosixPath' is not iterable
Run Code Online (Sandbox Code Playgroud)

提出一些想法,我该如何解决它。

这是我的 setting.py 文件

import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# 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 = '!25%rdt-$_$bsc*fl)e7x2*x6awjca^3_2t-k@l0tu*8k!f33&'

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

ALLOWED_HOSTS = ['*']


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

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

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR / 'static/')
Run Code Online (Sandbox Code Playgroud)

Mar*_*tin 10

如果您使用的是 Django v3.1,那么您可以尝试str()settings.py文件中放置数据库路径。这是因为 SQLite 只接受字符串并且 Django v3.1settings.py返回一个pathlib.Path对象。

# settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': str(BASE_DIR / 'db.sqlite3'),
    }
}
Run Code Online (Sandbox Code Playgroud)

我遇到了同样的问题,并在这里找到了答案:https : //forum.djangoproject.com/t/django-tutorial-python-manage-py-startapp-polls-fails/2718


小智 5

#在settings.py中执行

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': str(BASE_DIR / 'db.sqlite3'),
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是这样的

BASE_DIR = Path(__file__).resolve().parent.parent
Run Code Online (Sandbox Code Playgroud)

两者type(BASE_DIR)type(BASE_DIR / 'db.sqlite3')都是<class 'pathlib.PosixPath'>


小智 1

也许你有一个错字,“,”用了“/”。STATIC_ROOT = os.path.join(BASE_DIR, '静态')