我刚开始学习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, select / type_list.html异常位置:select_template中的C:\ Users \ Vato \ Envs \ django_test_env \ lib \ site-packages \ django \ template \ loader.py,第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要求我提供该模板 . Where in the code do I specify the need for it? 和 How can I fix it so that the program will get votes from database and display them on index?
正如我研究的那样,据我所知,第二个模板因为模型(类型)而被查看 - 由于某种原因,小写和_list结束 . 它是自动制作的,但我不明白 .
我不确定在我的代码中,因为从文档中复制了很多,但是我认为它应该没有第二个(type_list)模板 . 对不起,很长的帖子 . 思想不应该错过任何代码 .
如果您对更好的学习django的方法有任何建议,请随时发表评论 .
1 回答
你有
'DIRS': [BASE_DIR],
但你的模板不在BASE_DIR中,它们在BASE_DIR / templates中 . 你应该有: