django - 模板标签未在 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'),
]
解决方案
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',
],
},
},
]
推荐阅读
- python - 无法播放从 OpenAi 环境录制的视频
- nginx - 授权错误错误 400:nginx 负载均衡器后面的 Google Oauth 登录的 redirect_uri_mismatch
- r - 安装 setzler / eventStudy R 包给出错误“非零退出状态”
- m2doc - 如何将 java.util.Set 声明为 {m:template} 的入口参数
- makefile - 如何检查makefile中的文本可变长度?
- vue.js - 错误:创建 vue.js 项目时无法访问 jarfile /snap/vue/1/jar/vue-1.0-SNAPSHOT.jar
- vaadin - 带有 Bower 组件的 Vaadin 14:找不到模块错误
- php - 从他们的输入中存储将通过搜索查询与其他人共享的大量用户数据的常见做法是什么?
- pyopenssl - 如何在 pyOpenSSL 中使用 SAN 验证域名
- python - AWS Managed Airflow - 如何重新启动调度程序?