如何在django html模板中添加if函数
我想在django项目的html模板中添加一个如何在django html模板中添加if函数,html,css,django,Html,Css,Django,我想在django项目的html模板中添加一个if函数 如果变量数据为空,则显示文本“结果为空”,如果变量数据不为空,则显示数据中数据的表格 这是我写的,但它引起了错误 Invalid block tag on line 13: 'if(isEmpty($('#datas')))', expected 'endblock'. Did you forget to register or load this tag? 我该怎么处理呢 {% if(isEmpty($('#datas'))) %
if
函数
如果变量数据为空,则显示文本“结果为空”,如果变量数据不为空,则显示数据中数据的表格
这是我写的,但它引起了错误
Invalid block tag on line 13: 'if(isEmpty($('#datas')))', expected 'endblock'.
Did you forget to register or load this tag?
我该怎么处理呢
{% if(isEmpty($('#datas'))) %}
<h3>The results is null.</h3>
{% else %}
<table style="table-layout:fixed;">
<tr>...</tr>
<tr>
{% for i in datas %}
<td>{{ i.1 }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
{%if(isEmpty($('#datas')))%}
结果为空。
{%else%}
...
{数据%中的i为%1}
{{i.1}}
{%endfor%}
{%endif%}
我想你可以使用标签。像这样使用它:
<table style="table-layout:fixed;">
<tr>...</tr>
<tr>
{% for i in datas %}
<td>{{ i.1 }}</td>
</tr>
{% empty %}
<h3>The results is null.</h3>
{% endfor %}
def some_view(request):
context = {}
datas = Data.objects.all()
if datas.exists():
context['data_exists'] = True
context['datas'] = datas
return render(request, 'template.html', context)
并使用it模板:
{% if not data_exists %}
<h3>The results is null.</h3>
{% else %}
....
{%if not data_exists%}
结果为空。
{%else%}
....
您可以直接使用if条件
如果您是从视图发送数据
查看
def index(request):
datas = None
context = {"datas":datas}
return render(request, "index.html", context)
模板
{% if datas %}
<!-- your table -->
{% else %}
<h3>The results is null</h3>
{% endif %}
{%if数据%}
{%else%}
结果为空
{%endif%}