如何使用Django将此if语句添加到模板中并改进views.py
我对html和Django的了解非常有限,但我有一个Django项目,它有多(12)个html页面,在一个类似于下面代码的表格中显示信息。如果还需要进行任何修复,则会有条件地显示该表。如果修复中的任何数据已经过测试,则应以红色突出显示,以避免编辑。我最近添加了如何使用Django将此if语句添加到模板中并改进views.py,django,django-templates,Django,Django Templates,我对html和Django的了解非常有限,但我有一个Django项目,它有多(12)个html页面,在一个类似于下面代码的表格中显示信息。如果还需要进行任何修复,则会有条件地显示该表。如果修复中的任何数据已经过测试,则应以红色突出显示,以避免编辑。我最近添加了位来进行突出显示,但如果必须手动将其添加到所有12个页面中,那就太傻了。有没有办法将其添加为外部模板的一部分 {% extends "admin/base_site.html" %} {% load static %} {% block
位来进行突出显示,但如果必须手动将其添加到所有12个页面中,那就太傻了。有没有办法将其添加为外部模板的一部分
{% extends "admin/base_site.html" %}
{% load static %}
{% block content %}
<p><b>Description:</b> {{ docstring }}</p>
{% if count|length %}
<p><b>Action:</b> Investigate why and fix.</p>
<p style="color:red">If any table rows are highlighted red, indicating a tested profile, flag immediately to health report owner/Automation before editing.</p>
<table>
<tr bgcolor="#ccc">
<th>Profile</th>
<th>Name</th>
<th>Type</th>
<th>Tested</th>
<th>e.g. Device</th>
</tr>
{% for p in count %}
<tr {% if p.tested in green_tested %} bgcolor="red" {% endif %}>
<td><a href={{ p.profile_url }}>{{ p.url }}</a></td>
<td>{{ p.name }}</td>
<td>{{ p.type }}</td>
<td>{{ p.tested }}</td>
<td><a href={{ p.device_url }}>{{ p.dev_id }}</a></td>
{% endfor %}
</tr>
</table>
{% else %}
No fixes left to make!
{% endif %}
{% endblock %}
如果不必将'green\u tested'
添加到视图中的所有函数中,那也很好。{%extends“admin/base\u site.html”%}
{% extends "admin/base_site.html" %}
{% load static %}
{% block content %}
<p><b>Description:</b> {{ docstring }}</p>
{% if count|length !=0 %}
<p><b>Action:</b> Investigate why and fix.</p>
<p style="color:red">If any table rows are highlighted red, indicating a tested profile, flag immediately to health report owner/Automation before editing.</p>
<table>
<tr bgcolor="#ccc">
<th>Profile</th>
<th>Name</th>
<th>Type</th>
<th>Tested</th>
<th>e.g. Device</th>
</tr>
{% for p in count %}
<tr {% if p.tested in green_tested %} bgcolor="red" {% endif %}>
<td><a href="{{ p.profile_url }}">"{{ p.url }}"</a></td>
<td>{{ p.name }}</td>
<td>{{ p.type }}</td>
<td>{{ p.tested }}</td>
<td><a href={{ p.device_url }}>{{ p.dev_id }}</a></td>
{% endfor %}
</tr>
</table>
{% else %}
No fixes left to make!
{% endif %}
{% endblock %}
{%load static%}
{%block content%}
描述:{{docstring}}
{%如果计数|长度!=0%}
措施:调查原因并修复
如果任何表格行高亮显示为红色,表示已测试的配置文件,则在编辑之前立即向健康报告所有者/自动化部门标记
轮廓
名称
类型
测试
e、 g.装置
{计数%中的p的百分比}
{{p.name}}
{{p.type}}
{{p.tested}}
{%endfor%}
{%else%}
没有修复留下来做!
{%endif%}
{%endblock%}
对于每个视图中测试的绿色的问题,我建议使用上下文处理器。
首先,必须在view.py所在的同一位置创建一个新文件context_processors.py。诸如此类:
def function_name(request):
data = # here put logic you want
return {
'context_data': data
}
然后,您需要在模板中的settings.py文件中注册上下文处理器。模式是:YourAppName.FileName.FunctionName
这里的示例(我不替换YourAppName
,因为我不知道它):
然后,您可以在任何模板中使用{{context\u data}
,函数返回的字典中的键的名称
对于主要问题,我只有一个建议。您可以创建某种父模板,如“base_site.html”。base_site.html和当前模板之间的内容。但是你想放置的线条在中间的某个地方,所以我不确定这是否会为你工作,或者这些模板是如何彼此相似的。这可能比把那条线放12次更麻烦
{% extends "admin/base_site.html" %}
{% load static %}
{% block content1 %}
{% endblock %}
Here the code you want in each template.
{% block content2 %}
{% endblock %}
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['YourAppName/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',
# Here is new line
'YourAppName.context_processor.function_name',
],
},
},
]
{% extends "admin/base_site.html" %}
{% load static %}
{% block content1 %}
{% endblock %}
Here the code you want in each template.
{% block content2 %}
{% endblock %}