Django:AppRegistryNotReady() [英] Django: AppRegistryNotReady()

查看:112
本文介绍了Django:AppRegistryNotReady()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python:2.7; Django:1.7; Mac 10.9.4



我正在关注 Tango with Django



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



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



这是populate_rango.py:

  import os 

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

add_page(
cat = python_cat,
title =官方Python教程,
url =http: //docs.python.org/2/tutorial/


add_page(
cat = python_cat,
title =如何看待电脑科学家,
url =http://www.greenteapress.com/thinkpython/


add_page(
cat = python_cat,
title =在十分钟内学习Python,
url =http://www.korokithakis.net/tutorials/python/


django_cat = add_cat(Django)

add_page(
cat = django_cat,
title =官方Django教程,
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 =如何用Django探戈 ,
url =http://www.tangowithdjango.com/


frame_cat = add_cat(其他框架)

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


在Category.objects.all()中的c:
在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 = object.get_or_create(name = name)[0]
返回c

如果__name__ =='__main__':
打印启动Rango人口脚本...
os.environ.setdefault('DJANGO_SETTINGS_MODULE','tangle.settings')
from rango.models import Category,Page
populate()

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

  django.core.exceptions.AppRegistryNotReady 

然后我google了,发现像这个

 独立脚本¶
如果您在纯Python脚本(而不是管理命令)中使用Django,并且依赖于DJANGO_SETTINGS_MODULE环境变量,那么您必须在开始时显式初始化Django脚本:

>>> import django
>>> django.setup()
否则,您将遇到AppRegistryNotReady异常。

我还不知道该怎么办,可以帮忙吗? Thx !!!

解决方案

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



引用初始化过程文档:


setup()



自动调用此函数:




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


  • 调用
    管理命令时。




在其他情况下,必须明确调用
实例在纯Python脚本中。 >

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

 如果__name__ =='__main__':
打印启动Rango人口脚本...
os.environ。 setdefault('DJANGO_SETTINGS_MODULE','tangle.settings')

import django
django.setup()

populate()

此外,这个问题在疑难解答部分。


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

解决方案

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.

这篇关于Django:AppRegistryNotReady()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆