【问题标题】:TemplateDoesNotExist error in django polls aplicationdjango 民意调查应用程序中的 TemplateDoesNotExist 错误
【发布时间】: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

标签: html django


【解决方案1】:

在你的设置文件中你需要添加模板路径

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

【讨论】:

  • 对不起,我试过之后仍然遇到同样的错误。
【解决方案2】:

将您的应用添加到 settings.py 中的 INSTALLED_APPS 并在您的 TEMPLATES 设置中设置 'APPS_DIRS' = True

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'polls',
]
...

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

【讨论】:

    猜你喜欢
    • 2014-08-04
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2021-10-02
    • 2019-01-09
    • 1970-01-01
    相关资源
    最近更新 更多