Django无法识别设置模块

问题描述:

我试图按照this guide设置Django应用程序的设置模块。Django无法识别设置模块

我创建了设置模块,其中包含__init__ .py,common.py和dev.py.然而,当我试图python manage.py runserver --settings=settings.dev,我得到的导入错误:

如果我试图python manage.py runserver --settings={APP_NAME}.settings.dev,我得到的导入错误“没有命名为“设置模块”:

“无模块命名“共同”

我觉得我失去了一些东西简单在这里,但似乎无法弄清楚这里是我的文件的内容:

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

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

文件夹结构:here

+0

向我们展示您的文件夹结构 – sebb

+0

尝试将'from common import *'替换为'from .common import *'。并分享'__init __。py' – Rustem

+0

Rustem很有可能是正确的,并且您还在网上发布了您的密钥。虽然目前没有什么特别的协议,但希望不是很重要,从您发布的内容中删除敏感信息是一个好习惯(同样适用于您的数据库设置,它同时显示用户名和密码,尽管我们可以猜到这些:P ) – Ginnungagap

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

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

我使用Pycharm,并且必须进入编辑配置菜单来指定设置模块。对于任何人来说,这个答案应该仍然工作,不管。 – iso