DjangoアプリをRenderにデプロイすると、ModuleNotFoundError: No module named 'boardproject.settings' と表示される
解決したいこと
ローカル環境の場合、python manage.py runserver のコマンドでアプリが立ち上がるが、Renderにデプロイすると、'boardproject.settings' が読み込まれないエラーが発生。フォルダ構造とそれらしいファイルを貼ります。
発生している問題・エラー
ModuleNotFoundError: No module named 'boardproject.settings'
該当するソースコード
manage.py
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', "boardproject.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
wsgy.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', "boardproject.settings")
application = get_wsgi_application()
asgy.py
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'boardproject.settings')
application = get_asgi_application()
```manage.py
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', "boardproject.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
settings.py
from pathlib import Path
import os
#add
import environ
from decouple import config
from dj_database_url import parse as dburl
BASE_DIR = Path(__file__).resolve().parent.parent
env = environ.Env()
env.read_env(os.path.join(BASE_DIR, ".env"))
#
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
#add
#
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/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
# Application definition
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'boardapp.apps.BoardappConfig',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # 追加
'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 = 'boardproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
WSGI_APPLICATION = 'boardproject.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
default_dburl = "sqlite:///" + str(BASE_DIR / "db.sqlite3")
DATABASES = {
"default": config("DATABASE_URL", default=default_dburl, cast=dburl),
}
"""追加
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
#"""
# Password validation
# https://docs.djangoproject.com/en/4.2/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/4.2/topics/i18n/
#追加
#
LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'
USE_I18N = True
USE_TZ = True
#add
default_dburl = "sqlite:///" + str(BASE_DIR / "db.sqlite3")
#
#Default
STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
#
STATIC_ROOT = str(BASE_DIR / "staticfiles")
#追加
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
SUPERUSER_NAME = env("SUPERUSER_NAME")
SUPERUSER_EMAIL = env("SUPERUSER_EMAIL")
SUPERUSER_PASSWORD = env("SUPERUSER_PASSWORD")
#
STATICFILES_DIRS = [str(BASE_DIR / 'static')]
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = 'media/'
LOGIN_URL = 'login'
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
ファイル構造
C:.
│ .env
│ .gitignore
│ build.sh
│ db.sqlite3
│ manage.py
│ render.yaml
│ requirements.txt
│
├─boardapp
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ urls.py
│ │ views.py
│ │ __init__.py
│ │
│ ├─migrations
│ │ │ 0001_initial.py
│ │ │ 0002_alter_boardmodel_good_alter_boardmodel_read_and_more.py
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ 0001_initial.cpython-310.pyc
│ │ 0002_alter_boardmodel_good_alter_boardmodel_read_and_more.cpython-310.pyc
│ │ __init__.cpython-310.pyc
│ │
│ └─__pycache__
│ admin.cpython-310.pyc
│ apps.cpython-310.pyc
│ models.cpython-310.pyc
│ urls.cpython-310.pyc
│ views.cpython-310.pyc
│ __init__.cpython-310.pyc
│
├─boardproject
│ │ asgi.py
│ │ settings.py
│ │ urls.py
│ │ wsgi.py
│ │ __init__.py
│ │
│ └─__pycache__
│ settings.cpython-310.pyc
│ urls.cpython-310.pyc
│ wsgi.cpython-310.pyc
│ __init__.cpython-310.pyc
│
├─media
│ bird.jpg
│
│
│
│
│
│
│
├─static
│ style.css
│
└─templates
base.html
create.html
detail.html
list.html
login.html
signup.html
自分で試したこと
以下のサイトを参考にして、実施したが、できない。
https://zenn.dev/hathle/books/django-render-book/viewer/00_first
分かる方いましたら、ぜひ教えてください。
0