【发布时间】:2018-08-25 02:03:40
【问题描述】:
我正在 django 中创建一个投票应用程序,但出现以下错误:
TemplateDoesNotExist at /polls/
这就是我的索引函数的样子:
def index(request):
latest_questions = Question.objects.order_by('-pub_date')[0:5]
context = {'latest_questions': latest_questions}
return render(request, "polls/template.html", context)
模板.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if latest_ques %}
<ul>
{% for question in latest_ques%}
<li><a href = '/polls/{{question_id}}/'><b>
{{question.ques_text}}</b></a></li>
{% endfor %}
</ul>
{% endif %}
</body>
</html>
在我的投票文件里面我有一个模板文件,里面有一个模板文件夹,里面有另一个投票文件夹,里面有 template.html。
我尝试使用 render_to_response 代替渲染,我还尝试在 settings.py 中添加到 DIRS 的路径,并尝试将请求从函数中取出。太感谢了。
【问题讨论】:
-
你从哪里得到这个错误:
TemplateDoesNotExist at /polls/Browser OR Terminal? -
你需要从settings.py分享你的文件夹结构和
TEMPLATES -
是民意调查中模板目录中的 template.html 是 Django 所期望的,除非您更改了设置
-
你的问题答案在这里。请检查这个答案。 stackoverflow.com/a/3095114/5751530