Python Django:AppRegistryNotReady()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24793351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:12:24  来源:igfitidea点击:

Django: AppRegistryNotReady()

pythondjangodjango-1.7

提问by user2988464

Python: 2.7; Django: 1.7; Mac 10.9.4

蟒蛇:2.7;姜戈:1.7;Mac 10.9.4

I'm following the tutorial of Tango with Django

我正在学习DjangoTango教程

At Chapter 5, the tutorial teaches how to create a population script, which can automatically create some data for the database for the ease of development.

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

I created a populate_rango.py at the same level of manage.py.

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

Here's the 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()

Then I run python populate_rango.pyat the terminal at the level of manage.py, AppRegistryNotReady() is raised:

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

django.core.exceptions.AppRegistryNotReady

Then I googled it, found something like this:

然后,我用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.

And I still have no idea what should I do, can some one help? Thx!!!

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

采纳答案by alecxe

If you are using your django project applications in standalone scripts, in other words, without using manage.py- you need to manually call django.setup()first - it would configure the logging and, what is important - populate apps registry.

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

Quote from Initialization processdocs:

引用初始化过程文档:

setup()

This function is called automatically:

  • When running an HTTP server via Django's WSGI support.

  • When invoking a management command.

It must be called explicitly in other cases, for instance in plain Python scripts.

设置()

这个函数是自动调用的:

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

  • 调用管理命令时。

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

In your case, you need to call setup()manually:

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

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

    import django
    django.setup()

    populate()

Also, this problem is described in detail in Troubleshootingsection.

此外,此问题在故障排除部分中有详细描述。

回答by Valentin Kantor

I found this solution, adding

我找到了这个解决方案,添加

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

after

os.environ.setdefault ...

回答by Chris Wood

I also encountered this problem using Django 1.7 on an Apache server. Changing the wsgihandler call in my wsgi.pyfile fixed the problem:

我在 Apache 服务器上使用 Django 1.7 也遇到了这个问题。更改wsgi我的wsgi.py文件中的处理程序调用修复了问题:

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

This was suggested hereby user 'jezdez'.

这是用户 'jezdez'在这里建议的。

回答by normic

I just stumbled about the same problem in my local development server.

我只是在本地开发服务器中偶然发现了同样的问题。

After pulling some changed code in, the error was thrown. The problem here obviously has nothing to do with wsgi, so I tried to run manage.py

在拉入一些更改的代码后,错误被抛出。这里的问题显然与wsgi无关,所以我尝试运行manage.py

A simple: python manage.pyreveals the real error cause.

一个简单的:python manage.py揭示真正的错误原因。

In my case a forgotten import of an external Django app.

在我的情况下,忘记导入外部 Django 应用程序。

Maybe this helps someone else out.

也许这可以帮助其他人。