“尚未加载应用”和“ django.core.exceptions.ImproperlyConfigured”在Django中? [英] "Apps aren't loaded yet" and "django.core.exceptions.ImproperlyConfigured" in Django?

查看:128
本文介绍了“尚未加载应用”和“ django.core.exceptions.ImproperlyConfigured”在Django中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Django项目的目录结构。当我运行导入模型的python代码:<< c $ c>从scraping.models导入LinkVendorStandard 从文件 framework_product_processing.py时,抛出此异常:

This is the directory structure of my Django project. When I am running python code of importing a model:from scraping.models import LinkVendorStandard from the file "framework_product_processing.py" it throws this exception:

django.core.exceptions.ImproperlyConfigured:请求的设置为DEFAULT_INDEX_TABLESPACE,但未配置设置。在访问设置之前,您必须定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。

添加此代码时:

导入django
django.setup()

初始化django项目设置此异常: django.core.exceptions.AppRegistryNotReady:应用尚未加载。

to initialize the django project settings, I get this exception: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

我对此行为有以下两个问题:

I have the following 2 questions about this behavior:


  1. django项目结构中的文件 framework_product_process.py与 views.py处于同一级别,可以访问模型无需设置Django项目。如果可以从与视图相同的python路径访问此文件,那么为什么 django.core.exceptions.ImproperlyConfigured

  2. 即使在添加 import django; django.setup()代码之后,为什么我仍然得到 django.core.exceptions.AppRegistryNotReady:应用尚未加载。

  1. The file:"framework_product_process.py" in the django project structure is at the same level as "views.py" which can access model without having to setup the Django project.If this file is accessible from the same python path as that of view then why django.core.exceptions.ImproperlyConfigured?
  2. Even after adding import django;django.setup() code why I get django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.?

有人可以解释吗?

更新
当我将文件 framework_prodcut_processing.py移动到non_app python目录时,该文件可以正常运行。 non_app不是Django应用。

Update: The file "framework_prodcut_processing.py" runs without any errors when I move it to a non_app python directory. non_app is not a Django app.

推荐答案

django.core.exceptions.ImproperlyConfigured

当您运行 python manage.py runserver 之类的命令时,django会自动运行 django.setup 为您使用 DJANGO_SETTINGS_MODULE 环境变量。所以 views.py 中的代码可以访问模型,因为django确保在导入视图之前调用 django.setup 。由于您将外壳程序脚本作为简单的python文件运行,因此必须手动调用 django.setup

When you run commands like python manage.py runserver, django automatically runs django.setup for you using DJANGO_SETTINGS_MODULE environment variable. So the code in views.py can access models, because django ensures that django.setup is called before views are imported. Since you are running your shell script as a simple python file, so you must manually call django.setup.

django.core.exceptions.AppRegistryNotReady:应用尚未加载

通常会在导入应用程序之前完成完整的设置文件导入(即在INSTALLED_APPS初始化之前)。因此,请确保您在设置文件中没有任何代码,该文件会从其他一些应用程序导入代码。

This generally happens when your app gets imported before the complete settings files are imported (i.e. before the initialization of INSTALLED_APPS). So make sure you don't have any code in settings file, which imports code from some other apps.

还要确保您没有在应用程序的 __ init __。py 文件中导入模型或类似的应用程序代码。

Also ensure that you are not importing models or similar app code, in __init__.py files of your apps.

这篇关于“尚未加载应用”和“ django.core.exceptions.ImproperlyConfigured”在Django中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆