首页 > 解决方案 > 模板标签未在 HTML 中呈现

问题描述

我正在构建一个骨架 django 网站,但无法获得模板标签来渲染我认为是一个简单的测试。目前,模板标签只显示为文本(即,当我查看网站时,它只显示“{{ test }} and end”。

我确信问题很明显,但是我对 django 不是很有经验。我没有遇到任何错误,所以如果有任何调试建议会有所帮助。

如果我遗漏了任何关键信息,请告诉我。

视图.py

from django.shortcuts import render
from .models import Forms

def home(request):
    return render(request, 'kb/home.html', {'test': 'begining'})

主页.html

<html>
    <head>
        <title>Kinbank</title>
    </head>
    <body>
        <p>Hi there!</p>
        <p>{{ test }} and end </p>
    </body>
</html>

网址.py

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

urlpatterns = [
    path('', views.home, name='home'),
]

标签: djangodjango-templates

解决方案


settings.py您是否在该部分的文件中注册了您的应用程序INSTALLED_APPS?我复制/粘贴了您的代码,只要您注册了您的应用程序,它就对我有用。

您还需要确保您的app/templates文件夹中有模板。

最后,确保您的settings.py文件中有APP_DIRS = True. 这样 Django 将遍历您的应用程序以查找模板文件夹并找到您的 html 文件。

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

推荐阅读