如果使用select的值,如何创建一个if? [英] How can I create a if using the value of select?
本文介绍了如果使用select的值,如何创建一个if?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我有以下代码:
<select name="test1" class="form-control">
<option value=""></option>
{% for test in tests %}
<option value="{{ test.id }}">{{ test.name }}</option>
{% endfor %}
</select>
然后我想做这样的事情;
Then I want to do something like this ;
{% if test.id %}
# show something but I need to know if there is sometinh in test.id
# I think the problem comes from this line, in fact I want this condition is realised if test.id is not empty else the condition is not realised.
{% endif %}
我该怎么做?
推荐答案
看看这种方法是否有帮助.
See if this approach can help.
<select name="test1" class="form-control">
<option value=""></option>
{% for test in tests %}
<option value="{{ test.id }}">{{ test.name }}</option>
{% if test.id %}
#show what you want..
{% empty %} <<<<======
#if empty do this...
{% endif %}
{% endfor %}
</select>
只是一个例子:
import Http Response from django
from django.shortcuts import render
#create a function
def your_view(request):
# create a dictionary
context = {
#enter code here
"data" : [],
}
# return response
return render(request, "template.html", context)
现在在templates/template.html中,
Now in templates/template.html,
{% for i in data %}
<div class="row">
{{ i }}
</div>
{% empty %}
<h4>There is nothing in this list</h4>
{% endfor %}
您可以更好地查看此链接:> https://www.geeksforgeeks.org/for-empty-loop-django-template-tags/
You can have a better look at this link: https://www.geeksforgeeks.org/for-empty-loop-django-template-tags/
这篇关于如果使用select的值,如何创建一个if?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文