Django: AppRegistryNotReady()

2019-01-07 11:21发布

Python: 2.7; Django: 1.7; Mac 10.9.4

I'm following the tutorial of Tango with Django

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.

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

Here's the 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.py at the terminal at the level of manage.py, AppRegistryNotReady() is raised:

django.core.exceptions.AppRegistryNotReady

Then I googled it, found something like this:

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!!!

4条回答
看我几分像从前
2楼-- · 2019-01-07 11:46

I found this solution, adding

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

after

os.environ.setdefault ...
查看更多
家丑人穷心不美
3楼-- · 2019-01-07 11:47

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

A simple: python manage.py reveals the real error cause.

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

Maybe this helps someone else out.

查看更多
4楼-- · 2019-01-07 12:01

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.

Quote from Initialization process docs:

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.

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

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 Troubleshooting section.

查看更多
干净又极端
5楼-- · 2019-01-07 12:04

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

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

This was suggested here by user 'jezdez'.

查看更多
登录 后发表回答