Django模板和应用程序未加载 [英] Django Templates and Apps Not Loading

查看:62
本文介绍了Django模板和应用程序未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The title says most of it. I'm running Unittests.py in PyCharm on a tutorial and my templates folder is nested directly beneath my app folder (superlists/lists/templates/home.html). So here's the test I'm running 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)

And here's the code it's being run on (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.

So, I checked two places in settings.py, INSTALLED_APPS and 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.py views.py 的源代码以具有home.html路径的大多数组合*更改INSTALLED_APPS和TEMPLATES中列出的路径,以使 lists home.html 的路径具有不同长度的大多数组合*将'DIRS':[] 更改为'DIRS':[os.path.join(BASE_DIR,'templates')],

Here's a short list of things I've tried:
*Changing the source code of tests.py and views.py to have most combinations of the path to home.html *Changing the path listed in INSTALLED_APPS and TEMPLATES to have most combinations of varying lengths to the paths of lists and home.html *Changing 'DIRS': [] to 'DIRS': [os.path.join(BASE_DIR, 'templates')],

旁注:我还不得不反复使用

Sidenote: I've also had to repeatedly put DJANGO_SETTINGS_MODULE=(project directory name).settings as an environment variable in PyCharm, using the solution to an error, here, because the other solutions didn't work, so, I recognize that my setup may be just a tad buggy.

推荐答案

您的问题与模板无关,但PyCharm中的测试配置错误.

Your problem isn't related to template, but wrong test configuration in PyCharm.

似乎您正在使用 Python测试->针对您的测试的Unittests 配置,PyCharm为 Django测试提供了特定的配置(它会自动加载正确的django设置).

It seems you are using Python tests -> Unittests configuration for your tests, PyCharm provides a specific configuration for Django tests (it automatically loads the right django settings).

因此,请按照以下步骤进行配置:

So follow this steps to configure it:

  1. 转到菜单运行->编辑配置...
  2. 点击绿色的加号图标
  3. 选择 Django测试
  4. (可选)在 Target (在您的情况下为 lists )字段中输入Django应用名称
  1. Go to the menu Run -> Edit Configurations...
  2. Click on the green plus icon
  3. Select Django tests
  4. Optionally enter the Django app name in the Target field (lists in your case)

如果您已在PyCharm设置中配置了django项目和python解释器,则它将正常工作.

If you have configured your django project and python interpreter in your PyCharm settings it should work.

希望这会有所帮助.

这篇关于Django模板和应用程序未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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