gpt4 book ai didi

python - Django:AppRegistryNotReady()

转载 作者:IT老高 更新时间:2023-10-28 21:47:18 28 4
gpt4 key购买 nike

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="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()

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

django.core.exceptions.AppRegistryNotReady

然后我用谷歌搜索,发现类似 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.

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

最佳答案

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

引自 Initialization process文档:

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.

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

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

import django
django.setup()

populate()

另外,这个问题在 Troubleshooting 中有详细描述。部分。

关于python - Django:AppRegistryNotReady(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24793351/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com