Views.pyのようにfrom shop.models import Product と 記述してインポートしたいのですが
ModuleNotFoundError: No module named 'shop'
や
ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
のようなエラーが発生します。どなたか解決方法教えて頂けるとありがたいです。
1番下にバージョン記述しています。
実行環境はJupyter Lab(.ipynbファイル)にて行っております。
ターミナルでも試してみましたが同じエラーでした。
Python
1#<app/shop/views.py> 2 3from django.shortcuts import render 4 5from shop.models import Product 6 7import code 8console = code.InteractiveConsole(locals=locals()) # <- locals=locals() が重要 9console.interact() 10 11 12def all_products(request): 13 products = Product.valid_objects.all() 14 return render(request, 'shop/product_list.html', {'products': products}) 15
Python
1#<app/shop/models.py> 2 3from django.db import models 4 5 6class ValidManager(models.Manager): 7 def get_query(self): 8 return super(ValidManager, self).get_queryset().filter(available=True) 9 10 11class Category(models.Model): 12 name = models.CharField(max_length=250, unique=True) 13 slug = models.SlugField(max_length=250, unique=True) 14 description = models.TextField(blank=True) 15 image = models.ImageField(upload_to='category', blank=True) 16 17 class Meta: 18 ordering = ('name',) 19 verbose_name = 'category' 20 verbose_name_plural = 'categories' 21 22 def __str__(self): 23 return '{}'.format(self.name) 24 25class Product(models.Model): 26 name = models.CharField(verbose_name='商品名', max_length=250, unique=True) 27 slug = models.SlugField(verbose_name='URLに表示される名前', max_length=250, unique=True) 28 description = models.TextField(verbose_name='説明', blank=True) 29 category = models.ForeignKey(Category, on_delete=models.CASCADE) 30 price = models.DecimalField(max_digits=10, decimal_places=2) 31 image = models.ImageField(upload_to='product', blank=True) 32 stock = models.IntegerField() 33 available = models.BooleanField(default=True) 34 created = models.DateTimeField(auto_now_add=True) 35 updated = models.DateTimeField(auto_now=True) 36 37 objects = models.Manager() 38 39 valid_objects = ValidManager() 40 41 class Meta: 42 ordering = ('name',) 43 verbose_name = 'product' 44 verbose_name_plural = 'products' 45 46 def __str__(self): 47 return '{}'.format(self.name) 48
Python
1#<app/app/settings.py> 2 3import os 4 5# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 6BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 7 8 9# Quick-start development settings - unsuitable for production 10# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ 11 12# SECURITY WARNING: keep the secret key used in production secret! 13SECRET_KEY = 'yawt_&hx%)!6p7v8sbg^q*mu-9^to-b9tk_o8@)&#^ru0hp9=_' 14 15# SECURITY WARNING: don't run with debug turned on in production! 16DEBUG = True 17 18ALLOWED_HOSTS = [] 19 20 21# Application definition 22 23INSTALLED_APPS = [ 24 'shop', 25 26 'django.contrib.admin', 27 'django.contrib.auth', 28 'django.contrib.contenttypes', 29 'django.contrib.sessions', 30 'django.contrib.messages', 31 'django.contrib.staticfiles', 32] 33 34 35MIDDLEWARE = [ 36 'django.middleware.security.SecurityMiddleware', 37 'django.contrib.sessions.middleware.SessionMiddleware', 38 'django.middleware.common.CommonMiddleware', 39 'django.middleware.csrf.CsrfViewMiddleware', 40 'django.contrib.auth.middleware.AuthenticationMiddleware', 41 'django.contrib.messages.middleware.MessageMiddleware', 42 'django.middleware.clickjacking.XFrameOptionsMiddleware', 43] 44 45ROOT_URLCONF = 'app.urls' 46 47TEMPLATES = [ 48 { 49 'BACKEND': 'django.template.backends.django.DjangoTemplates', 50 'DIRS': [], 51 'APP_DIRS': True, 52 'OPTIONS': { 53 'context_processors': [ 54 'django.template.context_processors.debug', 55 'django.template.context_processors.request', 56 'django.contrib.auth.context_processors.auth', 57 'django.contrib.messages.context_processors.messages', 58 ], 59 }, 60 }, 61] 62 63WSGI_APPLICATION = 'app.wsgi.application' 64 65 66# Database 67# https://docs.djangoproject.com/en/2.1/ref/settings/#databases 68 69DATABASES = { 70 'default': { 71 'ENGINE': 'django.db.backends.postgresql', 72 'HOST': os.environ.get('DB_HOST'), 73 'NAME': os.environ.get('DB_NAME'), 74 'USER': os.environ.get('DB_USER'), 75 'PASSWORD': os.environ.get('DB_PASS'), 76 } 77} 78 79 80# Password validation 81# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators 82 83AUTH_PASSWORD_VALIDATORS = [ 84 { 85 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 86 }, 87 { 88 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 89 }, 90 { 91 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 92 }, 93 { 94 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 95 }, 96] 97 98 99# Internationalization 100# https://docs.djangoproject.com/en/2.1/topics/i18n/ 101 102LANGUAGE_CODE = 'ja' 103 104TIME_ZONE = 'Asia/Tokyo' 105 106USE_I18N = True 107 108USE_L10N = True 109 110USE_TZ = True 111 112 113# Static files (CSS, JavaScript, Images) 114# https://docs.djangoproject.com/en/2.1/howto/static-files/ 115 116STATIC_URL = '/static/' 117STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 118STATICFILES_DIRS = ( 119 os.path.join(BASE_DIR, 'static'), 120 ) 121MEDIA_URL = '/media/' 122MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media') 123
Python 3.9.7
Mac Monterey バージョン12.3.1
<requirements.txt>
Django>=2.1.3,<2.2.0
djangorestframework>=3.9.0,<3.10.0
flake8>=3.6.0,<3.7.0
psycopg2>=2.7.5,<2.8.0
Pillow>=5.3.0,<5.4.0
pytest-django==3.5.1
django-cors-headers==2.4.0
.
├── Dockerfile
├── Jupyter_Sample.ipynb
├── Makefile
├── app
│ ├── app
│ │ ├── init.py
│ │ ├── pycache
│ │ │ ├── init.cpython-39.pyc
│ │ │ └── settings.cpython-39.pyc
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── manage.py
│ ├── pytest.ini
│ ├── shop
│ │ ├── init.py
│ │ ├── pycache
│ │ │ ├── init.cpython-310.pyc
│ │ │ ├── init.cpython-39.pyc
│ │ │ ├── apps.cpython-39.pyc
│ │ │ ├── models.cpython-310.pyc
│ │ │ └── models.cpython-39.pyc
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── migrations
│ │ │ ├── 0001_initial.py
│ │ │ ├── 0002_product.py
│ │ │ └── init.py
│ │ ├── models.py
│ │ ├── templates
│ │ │ ├── base.html
│ │ │ ├── footer.html
│ │ │ ├── header.html
│ │ │ ├── navbar.html
│ │ │ └── shop
│ │ │ ├── product_detail.html
│ │ │ └── product_list.html
│ │ ├── tests
│ │ │ ├── test_models.py
│ │ │ └── test_views.py
│ │ ├── urls.py
│ │ └── views.py
│ └── static
│ ├── css
│ │ └── custom.css
│ ├── img
│ │ ├── apple.jpeg
│ │ ├── banner.jpg
│ │ └── logo.jpg
│ └── media
│ └── product
│ ├── apple.jpeg
│ ├── banana.jpeg
│ ├── kiwi.jpg
│ ├── orange.jpeg
│ └── pie.jpeg
├── docker-compose.yml
└── requirements.txt
