首页 » Python » 在使用django框架搭建web中如果需要单独运行py脚本出现AppRegist

在使用django框架搭建web中如果需要单独运行py脚本出现AppRegist

原文 http://blog.csdn.net/qq_35203425/article/details/79166394

2018-01-26 02:00:40阅读(270)

使用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.
 
解决方案:
在urls.py的开始加上
import os,django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
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_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

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

在使用django<a href=框架搭建web中如果需要单独运行py脚本出现AppRegist" src="http://img.blog.csdn.net/20180125204326184?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzUyMDM0MjU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">






最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹