AppRegistryNotReady:lazy format_html()?

时间:2016-04-13 14:52:05

标签: django

为什么我会收到此异常?

Traceback (most recent call last):
  File "/path1/myapp-isu/myapp_isu/tests/unit/views/test_view_isu.py", line 8, in <module>
    from myapp_isu.search_form import ISUSearchForm
  File "/path1/myapp-isu/myapp_isu/search_form.py", line 87, in <module>
    class ISUSearchForm(forms.Form):
  File "/path1/myapp-isu/myapp_isu/search_form.py", line 108, in ISUSearchForm
    foo_filter=forms.ModelChoiceField(FooFilter.objects.all(), label=format_html('<a href="%s">%s</a>', reverse_lazy('foo-filter'), FooFilter._meta.verbose_name))
  File "/path1/dt/dt/utils/templateutils.py", line 127, in reverse
    return urlresolvers.reverse(*args, **kwargs)
  File "/path1/dt/dt/utils/urlresolverutils.py", line 49, in patched_reverse
    base_url = orig_reverse(viewname, urlconf=urlconf, args=args, kwargs=kwargs, prefix=prefix, current_app=current_app)
  File "/path2/django/core/urlresolvers.py", line 578, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/path2/django/core/urlresolvers.py", line 432, in _reverse_with_prefix
    self._populate()
  File "/path2/django/core/urlresolvers.py", line 284, in _populate
    for pattern in reversed(self.url_patterns):
  File "/path2/django/core/urlresolvers.py", line 401, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/path2/django/core/urlresolvers.py", line 395, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/path1/myapp-eins/myapp_eins/etc/rooturls.py", line 13, in <module>
    admin.autodiscover()
  File "/path2/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/path2/django/utils/module_loading.py", line 67, in autodiscover_modules
    for app_config in apps.get_app_configs():
  File "/path2/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/path2/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

只有当我通过PyCharm调用unittest时才会发生这种情况,而不是在shell上使用py.test。

我猜reverse_lazy()在这里并不懒惰,因为它会在format_html()中使用。任何方式都有懒惰的format_html()

版本:

  • Django 1.8
  • Pycharm 5.0.4

4 个答案:

答案 0 :(得分:1)

由于堆栈跟踪中有类似url_patterns的内容,我假设DJANGO_SETTINGS_MODULE设置正确。

但是,在运行其他任何内容之前,您仍需要调用django.setup()

import django
django.setup()

对我而言,这使得此错误消息消失了。

答案 1 :(得分:0)

我自己使用PyCharm时遇到了一些麻烦,我假设您正在使用社区版(我一直在使用)。

如果是这样,问题很可能是你没有为django正确配置。你可以用一些可能适用于此的黑客来解决这个问题。

我从this.开始 (导入django以确保运行django控制台)

然后可能this.

还有这个: 检查&#34;编辑配置&#34;在测试中,您正在运行,并将DJANGO_SETTINGS_MODULE=<app-name-here>.settings添加到环境变量中。

答案 2 :(得分:0)

如果上述所有方法都失败了,请尝试初始化表单并在构造函数中执行导入:

    class ISUSearchForm(...):

    ...

    foo_filter = forms.Field()

    ...

    def __init__(*args, **kwargs)

      from ... import reverse_lazy
      from ... import FooFilter

      self.fields['foo_filter'] = \
        forms.ModelChoiceField(
          FooFilter.objects.all(),
          label=format_html(
            '<a href="%s">%s</a>',
            reverse_lazy('foo-filter'),
            FooFilter._meta.verbose_name
          )
        )

      super().__init__(*args, **kwargs)

错误可能是由不同测试运行程序执行的确切import序列引起的,也可能是因为您只使用类变量来自定义表单。

答案 3 :(得分:0)

在django 1.8中,reverse()和reverse_lazy()现在返回Unicode字符串而不是字节字符串。

请参阅链接:

https://docs.djangoproject.com/en/1.8/releases/1.8/