【问题标题】:raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configuredraise ImproperlyConfigured(django.core.exceptions.ImproperlyConfigured: 请求设置 INSTALLED_APPS,但未配置设置
【发布时间】:2021-11-23 07:22:37
【问题描述】:

提出不当配置( django.core.exceptions.ImproperlyConfigured:请求的设置 INSTALLED_APPS,但未配置设置。你必须要么 定义环境变量 DJANGO_SETTINGS_MODULE 或调用 settings.configure() 在访问设置之前。

无法找出错误的原因。

下面是我的settings.py:


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

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

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

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
import category.context_processors

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

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-(@pq@&r(t2tm_m8ci@$%z_z^l+kbnxdyd5#_+h8ckafos&8b5*'

# 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',
    'category',
    'accounts',
    'store',
]

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 = 'greatkart.urls'

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

WSGI_APPLICATION = 'greatkart.wsgi.application'

AUTH_USER_MODEL = 'accounts.Account'

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

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

# Password validation
# https://docs.djangoproject.com/en/3.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/3.2/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.2/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'
STATICFILES_DIRS = [
    'greatkart/static',
]

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# media files configuration

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

下面是我的上下文处理器:

from .models import Category
def menu_links(request):
    links = Category.objects.all()
    return dict(links=links)

以下是我的分类模型:

from django.db import models
from django.urls import reverse


# Create your models here.
class Category(models.Model):
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    description = models.TextField(max_length=255, blank=True)
    cat_image = models.ImageField(upload_to='photos/categories', blank=True)

    class Meta:
        verbose_name = 'Category'
        verbose_name_plural = 'Categories'

    def get_url(self):
        return reverse('products_by_Category', args=[self.slug])

    def __str__(self):
        return self.name

【问题讨论】:

  • 您必须错误地从 settings.py 中删除了起始引号。请在 settings.py 的开头添加"""。现在你所有的其他设置都只是作为字符串读取
  • 那个已经有了,不是贴错了。但是问题仍然存在。

标签: python django


【解决方案1】:

您必须删除 settings.py 中的起始双引号

【讨论】:

  • 没有解决问题
【解决方案2】:

请在settings.py中使用如下代码:

""" greatkart 项目的 Django 设置。 由 'django-admin startproject' 使用 Django 3.2.9 生成。 有关此文件的更多信息,请参阅 https://docs.djangoproject.com/en/3.2/topics/settings/ 有关设置及其值的完整列表,请参阅 https://docs.djangoproject.com/en/3.2/ref/settings/ """ 从路径库导入路径 # 在项目内部构建路径,如下所示:BASE_DIR / 'subdir'。 BASE_DIR = 路径(__file__).resolve().parent.parent # 快速启动开发设置 - 不适合生产 # 见 https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ # 安全警告:将生产中使用的密钥保密! SECRET_KEY = 'django-insecure-t*t_%wa66s=h+zo0nw^#x)!s#omgu^m@hm^-9pb2u!ql90lfu5' # 安全警告:不要在生产中打开调试运行! 调试 = 真 ALLOWED_HOSTS = [] # 应用定义 安装应用程序 = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', '类别', # '账户', # '店铺', ] 中间件 = [ '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 = 'greatkart.urls' 模板 = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS':[BASE_DIR/'模板'], 'APP_DIRS':是的, '选项': { “上下文处理器”:[ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'category.context_processors.menu_links', ], }, }, ] WSGI_APPLICATION = 'greatkart.wsgi.application' # 数据库 # https://docs.djangoproject.com/en/3.2/ref/settings/#databases 数据库 = { '默认': { '引擎': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } # 密码验证 # https://docs.djangoproject.com/en/3.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', }, ] # 国际化 # https://docs.djangoproject.com/en/3.2/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = 真 USE_L10N = 真 USE_TZ = 真 # 静态文件(CSS、JavaScript、图像) # https://docs.djangoproject.com/en/3.2/howto/static-files/ STATIC_URL = '/静态/' STATIC_ROOT = BASE_DIR / '静态' STATICFILES_DIRS = [ '伟大的卡丁车/静态', ] # 默认主键字段类型 # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' MEDIA_URL = '/媒体/' MEDIA_ROOT = BASE_DIR / '媒体'

【讨论】:

    猜你喜欢
    • 2018-10-09
    • 2020-04-12
    • 2019-12-02
    • 2020-08-25
    • 2020-09-27
    • 2013-03-22
    • 1970-01-01
    • 2018-05-21
    • 2015-04-16
    相关资源
    最近更新 更多