zoukankan      html  css  js  c++  java
  • Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet

    在程序中要添加django.setup()

    整个程序如下所示

    import os
    import django
    
    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", "DjangoHelloworld.settings");
        django.setup()  #添加的代码
        from HelloWorld.models import Category,Page
        populate()
  • 相关阅读:
    新建存储过程
    c#List移除列表中的元素
    学习总结初篇:发现问题
    webpack的几个使用方法
    Does Lamda expression return value?
    委托的最佳科普解释
    正则表达式字符匹配攻略
    Beta冲刺 第一天
    ###Beta准备
    高校征信系统项目Postmortem结果
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/4183985.html
Copyright © 2011-2022 走看看