【问题标题】:Template tags not rendering in HTML模板标签未在 HTML 中呈现
【发布时间】:2020-07-13 13:17:31
【问题描述】:

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

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

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

views.py

from django.shortcuts import render
from .models import Forms

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

home.html

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

urls.py

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

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

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

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

    您还需要确保您的模板在您的 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',
                ],
            },
        },
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2020-12-19
      • 2011-09-01
      • 2010-09-20
      • 1970-01-01
      相关资源
      最近更新 更多