django - AppRegistryNotReady : lazy format_html()?

标签 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
  • 最佳答案

    由于堆栈跟踪中有 url_patterns 之类的东西,我假设 DJANGO_SETTINGS_MODULE设置正确。

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

    import django
    django.setup()
    

    对我来说,这使此错误消息消失了。

    关于django - AppRegistryNotReady : lazy format_html()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36602151/

    相关文章:

    python - Django objects.all() 空查询集,在 shell 中不为空

    Djangocollectstatic 不能与 django-pipeline 和 yui-compressor 一起使用

    django - Elasticsearch dsl术语过滤器不适用于字符串字段

    django - vue.js 和 django 中的访问控制允许来源问题

    css - 如何在 Django 静态文件中使用背景图像 css 属性

    python - Django 中 socialauth 的 NoReverseMatch 错误

    python - F() 表达式和从 F() 表达式创建的 timedelta 的总和

    html - 在 django 中渲染发布的表单时显示所选选项

    django - 尝试运行 yarn run build-assets 时出现 Webpack 错误

    python - 属性错误: 'ModelFormOptions' object has no attribute 'private_fields' error message