【问题标题】:Django Templates and Apps Not LoadingDjango 模板和应用程序未加载
【发布时间】:2016-06-04 14:52:16
【问题描述】:

标题说明了大部分内容。我在教程的 PyCharm 中运行 Unittests.py,我的模板文件夹直接嵌套在我的应用程序文件夹 (superlists/lists/templates/home.html) 下。 所以这是我正在运行的测试 superlists/lists/tests.py:

def test_home_page_returns_correct_html(self):
    request = HttpRequest()

    response = home_page(request)
    expected_html = render_to_string('home.html')

    self.assertEqual(response.content.decode(), expected_html)

这是运行它的代码(superlists/lists/views.py)

def home_page(request):
    return render(request, 'home.html')

这是错误:

   Error
Traceback (most recent call last):
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\utils.py", line 86, in __getitem__
    return self._engines[alias]
KeyError: 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Me\PycharmProjects\superlists\lists\tests.py", line 19, in test_home_page_returns_correct_html
    response = home_page(request)
  File "C:\Users\Me\PycharmProjects\superlists\lists\views.py", line 8, in home_page
    return render(request, 'home.html')
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\shortcuts.py", line 67, in render
    template_name, context, request=request, using=using)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\loader.py", line 96, in render_to_string
    template = get_template(template_name, using=using)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\loader.py", line 26, in get_template
    engines = _engine_list(using)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\loader.py", line 143, in _engine_list
    return engines.all() if using is None else [engines[using]]
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\utils.py", line 110, in all
    return [self[alias] for alias in self]
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\utils.py", line 110, in <listcomp>
    return [self[alias] for alias in self]
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\utils.py", line 101, in __getitem__
    engine = engine_cls(params)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\backends\django.py", line 31, in __init__
    options['libraries'] = self.get_templatetag_libraries(libraries)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\backends\django.py", line 49, in get_templatetag_libraries
    libraries = get_installed_libraries()
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\backends\django.py", line 121, in get_installed_libraries
    for app_config in apps.get_app_configs())
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\apps\registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\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.**

所以,我检查了 settings.py 中的两个地方,INSTALLED_APPS 和 TEMPLATES。

这是两个部分:

    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'lists',
]

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

以下是我尝试过的简短列表:
*将tests.pyviews.py的源代码更改为home.html路径的大多数组合 *将 INSTALLED_APPS 和 TEMPLATES 中列出的路径更改为具有 listshome.html 路径的大多数不同长度的组合 *将'DIRS': [] 更改为'DIRS': [os.path.join(BASE_DIR, 'templates')],

旁注:我还不得不反复将DJANGO_SETTINGS_MODULE=(project directory name).settings 作为PyCharm 中的环境变量,使用错误的解决方案here,因为其他解决方案不起作用,所以,我认识到我的设置可能只是个小问题。

【问题讨论】:

  • 你能从命令行运行测试吗?即python manage.py test?
  • 嗯。奇怪的是,是的,并且测试通过了,除了我的字符串参数上的标记外,不推荐使用 url。 PyCharm 社区在惹我? *或单元测试?
  • 好的,还有几个问题: 1. 您是否为您的项目添加了 Django 支持?请参阅this page 的底部。和 2. 你在使用 Django 测试配置吗?见this page
  • 我没有 - 我一直在使用 this page 上的 Pycharm 社区版本的一些解决方案,假设测试框架会正常运行。我猜错了。现在是时候回到 CMD 了。谢谢。

标签: python django unit-testing templates pycharm


【解决方案1】:

您的问题与模板无关,而是 PyCharm 中的测试配置错误。

您的测试似乎使用Python tests -&gt; Unittests 配置,PyCharm 为Django tests 提供了特定配置(它会自动加载正确的 django 设置)。

所以请按照以下步骤进行配置:

  1. 转到菜单运行 -> 编辑配置...
  2. 点击绿色加号图标
  3. 选择 Django 测试
  4. 可以选择在 Target 字段中输入 Django 应用名称(在您的情况下为lists

如果您在 PyCharm 设置中配置了 django 项目和 python 解释器,它应该可以工作。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 2017-03-30
    • 2015-11-11
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多