在使用django框架搭建web中如果需要单独运行py脚本出现AppRegistryNotReady(Apps aren‘t loaded yet.)问题

在使用django框架搭建web中如果需要单独运行py脚本出现AppRegistryNotReady(“Apps aren’t loaded yet.”)问题

使用django2.0时发现能够访问web,但是单独运行某一个py文件时会出现如下错误:
Traceback (most recent call last):
  File "D:/Python_work/pycharm/py_work/mysite/mysite/urls.py", line 35, in <module>
    path('admin/', admin.site.urls),
  File "D:\Anaconda3\lib\site-packages\django\contrib\admin\sites.py", line 285, in urls
    return self.get_urls(), 'admin', self.name
  File "D:\Anaconda3\lib\site-packages\django\contrib\admin\sites.py", line 237, in get_urls
    from django.contrib.contenttypes import views as contenttype_views
  File "D:\Anaconda3\lib\site-packages\django\contrib\contenttypes\views.py", line 2, in <module>
    from django.contrib.contenttypes.models import ContentType
  File "D:\Anaconda3\lib\site-packages\django\contrib\contenttypes\models.py", line 134, in <module>
    class ContentType(models.Model):
  File "D:\Anaconda3\lib\site-packages\django\db\models\base.py", line 100, in __new__
    app_config = apps.get_containing_app_config(module)
  File "D:\Anaconda3\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config
    self.check_apps_ready()
File "D:\Anaconda3\lib\site-packages\django\apps\registry.py", line 127, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

解决方案:

在views.py的开始加上

import os,django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")    #mysite.settings为你得app应用名字
django.setup() 

然后单独运行改脚本就不出出错了。

如果在命令行中需要导入其他py模块,这是需要先导入该urls模块,不然出现导入不成功的情况:

会出现一下错误

from learn.models import Person

Traceback (most recent call last):
  File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code

    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-8-7faa32097dbe>", line 1, in <module>
    from learn.models import Person
  File "D:\Python_work\pycharm\PyCharm Community Edition 2017.1.5\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "D:\Python_work\pycharm\py_work\mysite\learn\models.py", line 7, in <module>
    class Person(models.Model):
  File "D:\Anaconda3\lib\site-packages\django\db\models\base.py", line 100, in __new__
    app_config = apps.get_containing_app_config(module)
  File "D:\Anaconda3\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config
    self.check_apps_ready()
  File "D:\Anaconda3\lib\site-packages\django\apps\registry.py", line 127, in check_apps_rea
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

但是先导入该urls模块就能成功

import mysite.urls
from learn.models import Person
from learn.models import Person
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值