AppRegistryNotReady:lazy format_html()? [英] AppRegistryNotReady: lazy format_html()?

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

问题描述

为什么我得到这个例外?

Why do I get this exception?

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

It only happens if I call the unittest via PyCharm, not if I use py.test on the shell.

我猜这个$ code> reverse_lazy()在这里不懒,因为它被用在 format_html()。任何方式拥有一个懒惰的 format_html()

I guess reverse_lazy() is not lazy here, since it gets used in format_html(). Any way to have a lazy format_html()?

版本:


  • Django 1.8

  • Pycharm 5.0.4

推荐答案

由于stacktrace中有url_patterns这样的东西,我假设 DJANGO_SETTINGS_MODULE 设置正确。

Since there are things like url_patterns in the stacktrace, I assume that DJANGO_SETTINGS_MODULE is set correctly .

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

But, you still need to call django.setup() before running anything else.

import django
django.setup()

对我来说,这个错误信息消失了。

For me, that made this error message go away.

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

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