Django无法识别设置模块

时间:2017-01-23 23:31:21

标签: python django

我试图按this guide为Django应用设置模块。

我创建了设置模块,其中包含 __ init __ .py,common.py和dev.py.但是,当我尝试python manage.py runserver --settings=settings.dev时,我收到导入错误:

"没有名为'设置的模块"

如果我尝试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     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/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是项目的名称;我试过删除它并使用" settings.dev'在manage.py和wsgi.py中,但没有运气。

这里有一个类似的主题,但问题似乎有所不同。

文件夹结构: here

1 个答案:

答案 0 :(得分:0)

如果您尝试为项目使用不同的设置模块,我建议您尝试添加" DJANGO_SETTINGS_MODULE"在你的wsgi.py文件中。在这样做之前和之后我遇到了这个问题它正常工作。我希望这能解决你的问题。

sys.path.append('/path/to/project)
os.environ["DJANGO_SETTINGS_MODULE"] = 'your_settings_module'