【问题标题】:"Can't find template" error in DjangoDjango中的“找不到模板”错误
【发布时间】:2015-06-25 10:28:37
【问题描述】:

我刚开始学习 Django。我按照 Django 网页上的指南进行操作,但仍然觉得不明白。所以我决定自己做一些类似的东西。基本上我正在制作与指南中类似的投票系统,但有点不同。所以我开始阅读一些文档和指南文本。我创建了一个通用列表视图来显示 index.html,它将显示投票列表。 '

这是我的观点代码:

class IndexView(generic.ListView):
    template_name = 'Vote/index.html'
    model = Type

    def get_queryset(self):
        return Type.objects.order_by('-pub_date')

这是我的 index.html 代码:

{% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static 'Vote/style.css' %}" />
{% block content %}
    <h2>Votings</h2>
    <ul>
        {% for voting in object_list %}
            <li>{{ voting.Type_name }}</li>
        {% empty %}
            <li>Sorry, there are not votes.</li>
        {% endfor %}
    </ul>
{% endblock %}

型号代码:

from django.db import models
from django.utils import timezone

# Create your models here.
class Voted_Object(models.Model):
    Voted_Object_name = models.CharField(max_length=50)
    Voted_Object_image = models.ImageField
    Voted_Object_rating = models.IntegerField(default=0)

class Type(models.Model):
    pub_date = models.DateTimeField
    Type_name = models.CharField(max_length=50)

网址代码:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    #url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
   # url(r'^(?P<pk>[0-9]+)/voting/$', views.VoteView.as_view(), name='voting'),
]

还有模板的设置:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR],
        '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',
            ],
        },
    },
]

这是我的目录:

最后这是错误:

TemplateDoesNotExist at /Vote/

Vote/index.html, Vote/type_list.html 请求方式:GET请求 URL:http://127.0.0.1:8000/Vote/ Django 版本:1.8.2 异常 类型:TemplateDoesNotExist 异常值:Vote/index.html, 投票/type_list.html 异常 位置:C:\Users\Vato\Envs\django_test_env\lib\site-packages\django\template\loader.py 在 select_template,第 76 行 Python 可执行文件:C:\Users\Vato\Envs\django_test_env\Scripts\python.exe Python 版本:2.7.10 Python 路径: ['C:\Users\Vato\PycharmProjects\Project3', 'C:\Users\Vato\PycharmProjects\Project3', 'C:\windows\SYSTEM32\python27.zip', 'C:\Users\Vato\Envs\django_test_env\DLLs', 'C:\Users\Vato\Envs\django_test_env\lib', 'C:\Users\Vato\Envs\django_test_env\lib\plat-win', 'C:\Users\Vato\Envs\django_test_env\lib\lib-tk', 'C:\Users\Vato\Envs\django_test_env\Scripts', 'C:\Python27\Lib', 'C:\Python27\DLLs', 'C:\Python27\Lib\lib-tk', 'C:\Users\Vato\Envs\django_test_env', 'C:\Users\Vato\Envs\django_test_env\lib\site-packages']

它需要 2 个模板,一个是我自己编写的 index.html,第二个是 type_list.html

我认为错误是由于缺少 type_list.html 文件引起的,但我不明白为什么 django 要求我提供该模板。 我在代码中的什么地方指定了它的需要?我怎样才能修复它,以便程序从数据库中获取投票并将它们显示在索引上?

根据我的研究和理解,第二个模板是由于模型(类型)而被视为小写字母和 _list 因某种原因而结束。它是在某个地方自动制作的,但我不明白。

我不确定我的代码,因为很多是从文档中复制的,但我认为它应该在没有第二个(type_list)模板的情况下工作。对不起,很长的帖子。思想不应该错过任何代码。

如果您对学习 django 的更好方法有任何建议,请随时发表评论。

【问题讨论】:

  • 您是否已将应用添加到已安装应用列表中?
  • 是的,我绝对有。

标签: python django


【解决方案1】:

您有'DIRS': [BASE_DIR],,但您的模板不在 BASE_DIR 中,它们在 BASE_DIR/templates 中。你应该有:

'DIRS': [os.path.join(BASE_DIR, 'templates')],

【讨论】:

  • 成功了!多谢!你有什么学习 django 的秘诀吗?
猜你喜欢
  • 2011-10-05
  • 2021-10-25
  • 2018-01-02
  • 2012-07-10
  • 2015-03-14
  • 2020-05-27
  • 1970-01-01
相关资源
最近更新 更多