Django设置模块异常

时间:2019-05-10 11:15:34

标签: python django exception environment-variables

我遇到以下错误:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1741, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Apache24/htdocs/consult/accounts/models.py", line 7, in <module>
    from django.contrib.auth.models import AbstractUser
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\db\models\base.py", line 87, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\apps\registry.py", line 249, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\apps\registry.py", line 131, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\conf\__init__.py", line 57, in __getattr__
    self._setup(name)
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\conf\__init__.py", line 42, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\_pydevd_bundle\pydevd_xml.py", line 179, in _get_type
    if isinstance(o, t[0]):
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\utils\functional.py", line 213, in inner
    self._setup()
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\conf\__init__.py", line 42, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

我对Python编程非常陌生。请帮助我解决此问题,并让我知道如何设置PYTHON环境变量。

这是我的设置。py

INSTALLED_APPS =

[

'accounts',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_mysql',

]

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

模板=  [

{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['/templates/accounts/'],
    '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 ='consult.wsgi.application'

数据库=

{

 'default': {
     'ENGINE': 'django.db.backends.mysql',
     'NAME': 'suitecaredb',
     'USER': 'root',
     'PASSWORD': 'innovations',
     'HOST': 'localhost',
     'PORT': '3306',
     'OPTIONS': {
        'charset': 'utf8mb4',
    },
           'TEST': {
        'CHARSET': 'utf8mb4',
        'COLLATION': 'utf8mb4_unicode_ci',
    }
 }

}

AUTH_USER_MODEL =“帐户用户”

3 个答案:

答案 0 :(得分:1)

要使用Django环境设置打开python终端,请运行

python manage.py shell

然后,例如,可以使用 create_user()帮助函数创建一个新用户:

>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')

# At this point, user is a User object that has already been saved
# to the database. You can continue to change its attributes
# if you want to change other fields.
>>> user.last_name = 'Lennon'
>>> user.save()

下面的示例代码显示了如何从脚本添加用户。请注意,您需要将第2行的'project.settings'替换为您的项目名称。

project.setting指向您的项目文件,其中指定了已安装的应用程序,中间件,模板等。

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','project.settings')

import django
django.setup()

from django.contrib.auth.models import User

super_user = User.objects.create_user(username = 'john',
                                first_name = 'John',
                                last_name = 'Doe',
                                email = 'john.doe@mail.com',
                                password = 'test',
                                is_staff = True,
                                is_superuser=True
                                )

文档docs.djangoproject.com

中的更多信息

答案 1 :(得分:0)

设置中:

  

wsgi.py

检查它是否指向正确的设置路径:

  

os.environ.setdefault(“ DJANGO_SETTINGS_MODULE”,“路径/至/settings.py”)

答案 2 :(得分:0)

尝试以下方法:

  1. 如以上答案中所述,请确保wsgi.py

    中包含以下行

    os.environ.setdefault(“ DJANGO_SETTINGS_MODULE”,“ /path/to/settings.py”)

  2. 确保上述路径正确,或者上一行中提到的设置文件为实际设置文件。

  3. 由于您使用的是Pycharm,因此上述错误可能是由于Pycharm环境变量出现某些问题引起的。尝试python manage.py flush

  4. 如果问题仍然存在,请查看Pycharm设置中的环境配置,并检查是否存在差异。