【问题标题】:Q: Django ImproperlyConfigured - from django.contrib.auth.models import User问:Django 配置不当 - 从 django.contrib.auth.models 导入用户
【发布时间】:2022-01-21 12:50:50
【问题描述】:

我有一个名为 Veganet 的 Django 项目,我正在尝试运行它,但是当我运行我的主脚本 vega_flask_run.py 时,它给了我一个配置不正确的错误。错误的来源来自一个名为 models.py 的脚本,更具体地说是我从 django.contrib.auth.models import User 声明的第 2 行。我尝试使用一些帖子来解决我的问题,包括:

我尝试了一些解决方案,例如在我的项目 settings.py 中更改 INSTALLED_APPS 变量并更改 settings.py 中的 DATABASES 变量,以及使用 Windows Powershell 执行python manage.py shelldjango-admin.py shell --settings=mysite.settings,然后设置 DJANGO_SETTINGS_MODULE 变量指向我的项目设置,最后使用

from django.core.management import setup_environ
from veganet import settings

setup_environ(settings)

这是整个错误:

Exception has occurred: 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.
  File "C:\Users\trevo\OneDrive\Desktop\veganet\profiles\models.py", line 2, in <module>
    from django.contrib.auth.models import User
  File "C:\Users\trevo\OneDrive\Desktop\veganet\profiles\forms.py", line 2, in <module>
    from .models import ExperimentContext, Profile
  File "C:\Users\trevo\OneDrive\Desktop\veganet\vega_ai\VegaMain.py", line 25, in <module>
    from profiles.forms import ProfileModelForm, ExperimentModelForm
  File "C:\Users\trevo\OneDrive\Desktop\veganet\vega_flask_run.py", line 5, in <module>
    from vega_ai.VegaMain import app as frontend

这是我的简化文件结构:

veganet (main folder)
 -profiles (folder)
  ...
  models.py (script that gives me the error)
  signals.py
  urls.py
  utils.py
  views.py
 -vega_ai (folder)
  VegaMain.py 
  ...
 -vega_net (folder)
  ...
  asgi.py
  settings.py
  urls.py
  views.py
  wsgi.py
 db.sqlite3
 manage.py 
 vega_flask_run.py (where I am running the project)

我的 settings.py 文件:

from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
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/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-p8!i$5eqn)l(f0(n##1yntb^#ctfm3u*)j9xrjy^(1n9s&jdsa'

# 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',

    'posts',
    'profiles',
    'veganet',
]

LOGIN_URL = '/admin/'

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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ["C:/Users/trevo/OneDrive/Desktop/veganet/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 = 'veganet.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/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/3.0/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/3.0/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static_project')
]

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "static_root")

MEDIA_URL = '/media/'
MEDIA_ROOT = 'C:/Users/trevo/OneDrive/Desktop/veganet/static_cdn/media_root'

我的 wsgi.py 脚本:

import os
from django.conf import settings
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'veganet.settings')

application = get_wsgi_application()

models.py

from django.db import models
from django.contrib.auth.models import User
from django.dispatch.dispatcher import receiver
from .utils import get_random_code
from django.template.defaultfilters import slugify
import subprocess

...

vega_flask_run.py

from email.mime import application
from flask import Flask, render_template
from werkzeug.serving import run_simple
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from vega_ai.VegaMain import app as frontend
from manage import app as backend
from werkzeug.exceptions import NotFound
app = Flask(__name__, template_folder="C:/Users/trevo/OneDrive/Desktop/veganet/profiles/templates/profiles", static_folder="C:/Users/trevo/OneDrive/Desktop/veganet/static_project")


app.wsgi_app = DispatcherMiddleware(frontend, {
    '/app2':app,
    '/app3':backend
})

if __name__ == '__main__':
    app.run(debug=True,port=8080,use_reloader=False)

我到底错过了什么?谢谢

【问题讨论】:

    标签: python django


    【解决方案1】:

    如果这是 Django,而不是 vega_flask_run.py 名称所暗示的烧瓶,那么您需要运行 python manage.py runserver 来启动应用程序。

    这可能与Django ImproperlyConfigured for import User 重复。

    另外,models.py 不是您在 django 中运行的脚本,就像我认为您所说的那样。它是您为项目定义的模型、类所在的位置。

    我可能是那个错过了什么的人。也许您可以使用vega_flask_run.py 文件编辑您的问题。也许您正在以我不熟悉的方式使用它。

    否则,我建议你从https://docs.djangoproject.com/en/4.0/intro/tutorial01/开始。我的意思是看起来你已经正确地启动了项目,因为那里有 django 文件。也许这只是你如何启动它们的问题。

    编辑
    是的,现在我可以看到您正在尝试运行一个 djagno 应用程序,就好像它是一个烧瓶应用程序,app = Flask(__name__, ...。尽管 Flask 和 Django 都是 python 框架,但它们的运行方式非常不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2016-07-07
      相关资源
      最近更新 更多