即使路径设置正确,也可以使用TemplateDoesNotExist

时间:2018-07-27 05:47:27

标签: django html5 django-templates

Look and help
我认为代码中输入的路径是正确的,仍然表明模板不存在。

3 个答案:

答案 0 :(得分:0)

Django在templates文件夹中寻找模板,但建议在HTML应当驻留的模板文件夹中包含带有 Appname 的子文件夹。

因此将文件夹Snippet的名称更改为customer,然后您的链接应为:

{%include 'customer/shop_card.html' with instance=shop %}

或尝试:

{%include 'shop_card.html' with instance=shop %}

答案 1 :(得分:0)

这就是答案:-

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <div class="container">
  <ol class="list-timeline">
    <li>
      <h3 class="title">The Establishment</h3>
      <div class="content">
        <p>Ascetur ridiculus mus. Suspendisse pellentesque convallis massa.</p>
      </div>
    </li>
    <li>
      <h3 class="title">150 Employee</h3>
      <div class="content">
        <p>Pellentesque habitant morbi tristique senectus.</p>
      </div>
    </li>
  </ol>
</div>

用于搜索模板的默认文件夹是“ template / templates” 因此,您不必提供有关路径的其他详细信息。 如果您只写

            {% include 'snippets/shop_card.html' with instance=shop %}

它将在没有该文件的文件夹模板中进行搜索。

如果“模板/模板”中有任何文件夹,请指定该文件夹及其中文件的名称,在这种情况下为“ snippets / shop_card.html”。
希望对您有帮助。

答案 2 :(得分:0)

Django通过遵循“ TEMPLATES”设置查找模板。如果未指定并且设置中的“ TEMPLATES”->“ APP_DIRS”为True,则Django将查看每个应用程序中的“ templates”文件夹(默认情况下,模板必须位于文件夹“ app / templates / {app_name}”中,该文件夹为ans2human说来。)

因此,如果计划将模板放置在非默认位置,则最好在settings.py中设置模板->'DIRS'。例如

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        os.path.join(BASE_DIR, 'customers', 'templates'),
        os.path.join(BASE_DIR, 'other', 'place', 'templates'),
    ],
    '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',
        ],
    },
},

]

相关问题