【问题标题】:Django No module named 'static_root'Django没有名为'static_root'的模块
【发布时间】:2023-03-26 09:02:01
【问题描述】:

我正在尝试创建一个本地引导文件以使用 Django 呈现网站。在设置静态文件目录时,我修改了设置,现在我不断收到错误“ModuleNotFoundError: No module named 'static_root'”

如果我尝试注释掉 static_url 或 staticfiles_dirs 声明,错误不会消失。 settings.py 文件如下所示:

settings.py:

"""
Django settings for static_root project.

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

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

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


import os

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


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

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


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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'static_root',
    'app.apps.AppConfig',
    'nested_admin',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(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 = 'static_root.wsgi.application'


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

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'sensor_package',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'USER': 'root',
        'PASSWD': '',
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.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/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'CET'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

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

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


MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

文件夹结构如下:

Folder Structire

错误信息如下:

  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 936, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'static_root'

【问题讨论】:

  • 你为什么要命名一个类似于内置 Django 选项的应用程序?还是这是替换不准确的结果?
  • 您指的是“应用程序”。无论如何,我一开始就尝试按照教程进行操作,并将我的结构调整为那里提供的结构。但从那时起,该项目发生了很大变化
  • 哇,密钥公开了。考虑创建一个新的 ;)
  • @hansTheFranz,很好发现,谢谢!菜鸟失误

标签: django django-staticfiles


【解决方案1】:

您的INSTALLED_APPS 中有'static_root'。它不属于那里,因为它是一个目录(可能),而不是 Python 包。

编辑:它也在WSGI_APPROOT_URLCONF 中。如果你没有那个名字的包,那就错了。

【讨论】:

  • 使用 CTRLF+F。它无处不在。 @安德烈
  • 我认为它可能与 settings.py 文件无关,因为无论我注释掉包含“静态”部分的内容,都会出现相同的错误
  • 是的,它有。你的 settings.py 被毁了。 @安德烈
  • @AKX,谢谢,你是对的。我设法找到了一个旧版本,并意识到在某些时候所有项目文件中的所有“website.settings”都已更改为“static_root.settings”。如果有人知道为什么会这样,请告诉我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
  • 2013-07-17
  • 2012-06-02
  • 2019-03-08
  • 2021-08-06
  • 2020-11-14
相关资源
最近更新 更多