根据标题显示不同的内容
Displaying different content depending on which title it has
我正在创建一个基于分类视图的 Django 应用程序。在详细视图中,我想根据 object 的标题显示不同的内容。我不明白为什么 if 语句不起作用。示例中的数据目前只是虚拟数据,因为它处于开发阶段。
这显示了我创建的详细视图。
{% extends 'main.html' %}
{% block content %}
<div class="container px-5">
{{object.title}}
{% if object.title == 'hello' %}
{% include 'x/y.html' %}
{% endif %}
</div>
{% endblock content %}
有人知道我该如何解决这个问题吗?
这是我的 TEMPLATES DIR 设置。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
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',
],
},
},
]
而我的y/x.html和详情页存放在同一个目录下。在这种情况下,目录是“y”。
我会确保您的 settings.py
文件中的 TEMPLATES and BASE_DIR
变量看起来像这样:
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [str(BASE_DIR.joinpath('templates'))], # new
'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',
],
},
},
]
我正在创建一个基于分类视图的 Django 应用程序。在详细视图中,我想根据 object 的标题显示不同的内容。我不明白为什么 if 语句不起作用。示例中的数据目前只是虚拟数据,因为它处于开发阶段。
这显示了我创建的详细视图。
{% extends 'main.html' %}
{% block content %}
<div class="container px-5">
{{object.title}}
{% if object.title == 'hello' %}
{% include 'x/y.html' %}
{% endif %}
</div>
{% endblock content %}
有人知道我该如何解决这个问题吗?
这是我的 TEMPLATES DIR 设置。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
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',
],
},
},
]
而我的y/x.html和详情页存放在同一个目录下。在这种情况下,目录是“y”。
我会确保您的 settings.py
文件中的 TEMPLATES and BASE_DIR
变量看起来像这样:
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [str(BASE_DIR.joinpath('templates'))], # new
'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',
],
},
},
]