【发布时间】:2017-06-08 14:59:03
【问题描述】:
我正在尝试根据this guide 为 Django 应用程序设置一个设置模块。
我创建了设置模块,其中包含 __init__.py、common.py 和 dev.py。然而,当我尝试python manage.py runserver --settings=settings.dev 时,我得到了导入错误:
“没有名为‘settings’的模块”
如果我尝试python manage.py runserver --settings={APP_NAME}.settings.dev,则会收到导入错误:
"没有名为 'common' 的模块
我觉得我在这里遗漏了一些简单的东西,但似乎无法弄清楚。以下是我的文件内容:
dev.py:
from common import *
# from os.path import join, normpath
# DEBUG CONFIG
DEBUG = True
ALLOWED_HOSTS = []
# END DEBUG CONFIG
# DATABASE CONFIG
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Local instance wampmysqld64',
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
}
}
# END DATABASE CONFIG
common.py 导入操作系统
# 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/1.10/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
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'test',
]
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 = 'API_PG.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 = 'PG.wsgi.application'
# Password validation
# https://docs.djangoproject.com/en/1.10/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/1.10/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/1.10/howto/static-files/
STATIC_URL = '/static/'
manage.py:
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PG.settings.dev")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
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?"
)
raise
execute_from_command_line(sys.argv)
wsgi.py
import os
import sys
root_path = os.path.abspath(os.path.split(__file__)[0])
sys.path.insert(0, os.path.join(root_path, 'PG.settings.dev'))
sys.path.insert(0, root_path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PG.settings.dev")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
PG 是项目的名称;我尝试删除它并在 manage.py 和 wsgi.py 中仅使用“settings.dev”,但没有运气。
这里有一个类似的帖子,但问题似乎有所不同。
文件夹结构: here
【问题讨论】:
-
向我们展示您的文件夹结构
-
尝试将
from common import *替换为from .common import *。并分享__init__.py -
Rustem 很可能是对的,而且,您在网上发布了您的密钥。虽然没什么大不了的,因为此时它希望不是很重要,但从您发布的内容中删除敏感信息是一个好习惯(同样适用于显示用户名和密码的数据库设置,尽管我们可能已经猜到了:P )
-
@Rustem
from .common import *成为未使用的导入语句,__init__.py为空白 -
@sebb 我用底部的结构编辑了帖子,谢谢