python - 拆分 settings.py 文件时无法在 django 中启动新应用程序
问题描述
我这样拆分settings.py
:
# __init__.py
from .base import * # noqa
try:
from .local_settings import * # noqa
except ImportError:
message = 'ERROR: Unable to import local_settings. Make sure it exists.'
raise ImportError(message)
# base.py
"""
Django settings for app project.
Generated by 'django-admin startproject' using Django 3.0.4.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/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.dirname(os.path.abspath(__file__))))
# Add the apps folder to python path
os.sys.path.append(os.path.join(BASE_DIR, 'apps'))
# Application definition
INSTALLED_APPS = [
'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 = 'app.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'app.wsgi.application'
# Password validation
# https://docs.djangoproject.com/en/3.0/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',
},
]
和:
# local_settings.py
import os
from .base import BASE_DIR
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '670b9a538dfe8545f4eff723345da43211084a05f520a2d638'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'run/db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/3.0/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.0/howto/static-files/
STATIC_URL = '/static/'
每个命令都很好用,除了python manage.py startapp myapp [or any other app name] ./apps/myapp
. 每次我运行它命令时,我都会收到此错误:
CommandError: 'myapp' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
我发现它与我在__init__.py
文件中的代码有关。我的意思是,如果我评论所有代码,该错误将消失。为什么会这样?我该如何解决?
解决方案
正如@juggernaut 所说。当您使用字符时会发生错误,该字符*
会导入所有内容并因此覆盖一些内部内容。
当您运行命令时python manage.py startapp myapp xxxx
,该myapp
部分与现有模块冲突,因此会导致该错误。
尝试手动导入它而不是使用from .base import * # noqa
. 这样你只导入你需要的模块,不会导致任何错误。
推荐阅读
- cordova - 打开层 4.5 点击/触摸一个点(Android 设备)
- python - 无法根据字符串获取特定行
- javascript - 防止滚动模态框内容时滚动背景 div
- python-3.x - NameError:名称“合并”未定义
- angular - 无法绑定到 mat-table 中的数据
- java - 设置请求参数的大小限制
- python - Python,检查字符串的值并删除其他字符
- python - 为从 init 方法分配的类函数起别名
- java - 解析响应 xml 并计算 CDATA 中的 XML 元素
- java - 寻找素数的程序。第2章自测schildt java初学者指南