Django:AppRegistryNotReady()

use*_*464 37 python django django-1.7

Python:2.7; Django:1.7; Mac 10.9.4

我正在使用DjangoTango教程

在第5章中,本教程教授如何创建填充脚本,该脚本可以自动为数据库创建一些数据,以便于开发.

我在manage.py的同一级别创建了一个populate_rango.py.

这是populate_rango.py:

import os

def populate():
    python_cat = add_cat('Python')

    add_page(
        cat=python_cat,
        title="Official Python Tutorial",
        url="http://docs.python.org/2/tutorial/"
    )

    add_page(
        cat=python_cat,
        title="How to Think like a Computer Scientist",
        url="http://www.greenteapress.com/thinkpython/"
    )

    add_page(
        cat=python_cat,
        title="Learn Python in 10 Minutes",
        url="http://www.korokithakis.net/tutorials/python/"
    )

    django_cat = add_cat("Django")

    add_page(
        cat=django_cat,
        title="Official Django Tutorial",
        url="https://docs.djangoproject.com/en/1.5/intro/tutorial01/"
    )

    add_page(
        cat=django_cat,
        title="Django Rocks",
        url="http://www.djangorocks.com/"
    )

    add_page(
        cat=django_cat,
        title="How to Tango with Django",
        url="http://www.tangowithdjango.com/"
    )

    frame_cat = add_cat("Other Frameworks")

    add_page(
        cat=frame_cat,
        title="Bottle",
        url="http://bottlepy.org/docs/dev/"
    )

    add_page(
        cat=frame_cat,
        title="Flask",
        url="http://flask.pocoo.org"
    )

    for c in Category.objects.all():
        for p in Page.objects.filter(category=c):
            print "- {0} - {1}".format(str(c), str(p))


def add_page(cat, title, url, views=0):
    p = Page.objects.get_or_create(category=cat, title=title, url=url, views=views)[0]
    return p


def add_cat(name):
    c = Category.objects.get_or_create(name=name)[0]
    return c

if __name__ == '__main__':
    print "Starting Rango population script..."
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tangle.settings')
    from rango.models import Category, Page
    populate()
Run Code Online (Sandbox Code Playgroud)

然后我python populate_rango.py在manage.py级别的终端上运行,引发了AppRegistryNotReady():

django.core.exceptions.AppRegistryNotReady
Run Code Online (Sandbox Code Playgroud)

然后,我用Google搜索了一下,发现像这样:

Standalone scripts¶
If you’re using Django in a plain Python script — rather than a management command — and you rely on the DJANGO_SETTINGS_MODULE environment variable, you must now explicitly initialize Django at the beginning of your script with:

>>> import django
>>> django.setup()
Otherwise, you will hit an AppRegistryNotReady exception.
Run Code Online (Sandbox Code Playgroud)

我仍然不知道我该怎么做,有人可以帮忙吗?谢谢!!!

ale*_*cxe 58

如果您在独立脚本中使用django项目应用程序,换句话说,不使用manage.py- 您需要先手动调用django.setup()- 它将配置日志记录,重要的是 - 填充应用程序注册表.

初始化过程文档引用:

建立()

自动调用此函数:

  • 通过Django的WSGI支持运行HTTP服务器时.

  • 调用管理命令时.

必须在其他情况下显式调用它,例如在纯Python脚本中.

在您的情况下,您需要setup()手动调用:

if __name__ == '__main__':
    print "Starting Rango population script..."
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tangle.settings')

    import django
    django.setup()

    populate()
Run Code Online (Sandbox Code Playgroud)

此外,故障排除部分中详细介绍了此问题.