Django设置模块异常



我有以下错误:

Traceback (most recent call last):
  File "C:Program FilesJetBrainsPyCharm Community Edition 2019.1.1helperspydevpydevd.py", line 1741, in <module>
    main()
  File "C:Program FilesJetBrainsPyCharm Community Edition 2019.1.1helperspydevpydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:Program FilesJetBrainsPyCharm Community Edition 2019.1.1helperspydevpydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:Program FilesJetBrainsPyCharm Community Edition 2019.1.1helperspydev_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:Apache24htdocsconsultclinicenvlibsite-packagesdjangocontribauthmodels.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:Apache24htdocsconsultclinicenvlibsite-packagesdjangocontribauthbase_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "C:Apache24htdocsconsultclinicenvlibsite-packagesdjangodbmodelsbase.py", line 87, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:Apache24htdocsconsultclinicenvlibsite-packagesdjangoappsregistry.py", line 249, in get_containing_app_config
    self.check_apps_ready()
  File "C:Apache24htdocsconsultclinicenvlibsite-packagesdjangoappsregistry.py", line 131, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:Apache24htdocsconsultclinicenvlibsite-packagesdjangoconf__init__.py", line 57, in __getattr__
    self._setup(name)
  File "C:Apache24htdocsconsultclinicenvlibsite-packagesdjangoconf__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 FilesJetBrainsPyCharm Community Edition 2019.1.1helperspydev_pydevd_bundlepydevd_xml.py", line 179, in _get_type
    if isinstance(o, t[0]):
  File "C:Apache24htdocsconsultclinicenvlibsite-packagesdjangoutilsfunctional.py", line 213, in inner
    self._setup()
  File "C:Apache24htdocsconsultclinicenvlibsite-packagesdjangoconf__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',

]

中间件=[

'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 =" accounts.user"

打开使用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

中的更多信息

设置:

wsgi.py

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

os.environ.setDefault(" django_settings_module"," path/to/to/settings.py"(

尝试以下内容:

  1. 如上所述,请确保wsgi.py中存在以下行

    os.environ.setDefault(" django_settings_module","/path/path/to/settings.py"(

  2. 确保上述路径正确或上线中提到的设置文件是实际的设置文件。

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

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

最新更新