我刚开始学习Django。我遵循Django网页上的指南,但仍然不觉得它理解它。所以我决定自己做一些类似的事情。基本上我正在制作类似于指南的投票系统,但有点不同。所以我开始通过阅读一些文件和指导文本来做到这一点。我创建了一个通用的listview来显示index.html,它将显示投票列表。 “Django中的“无法找到模板”错误
这是我的看法代码:
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)
的url代码:
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/
Vote/index.html, Vote/type_list.html Request Method: GET Request URL: http://127.0.0.1:8000/Vote/ Django Version: 1.8.2 Exception Type: TemplateDoesNotExist Exception Value: Vote/index.html, Vote/type_list.html Exception Location: C:\Users\Vato\Envs\django_test_env\lib\site-packages\django\template\loader.py in select_template, line 76 Python Executable: C:\Users\Vato\Envs\django_test_env\Scripts\python.exe Python Version: 2.7.10 Python Path: ['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的问我该模板。 在代码中,我需要指定它吗?和我该如何修复它,以便程序将从数据库中获得投票并将它们显示在索引上?
为我的研究和我的理解第二个模板看着因为Model(型号)-to小写,由于某种原因_List结束。它是自动进行的,但我不明白。
我不知道在我的代码,造成多大的从单证复制,但我认为应该没有第二个(TYPE_LIST)模板工作过。对不起,很长的职位。思想不应该错过任何代码。
如果你有学习的Django,请随意评论的更好的办法任何建议。
您是否在已安装的应用程序列表中添加了应用程序? – nu11p01n73R
是的,我肯定有。 – Vato