Odi*_*aeb 1 django django-templates
到目前为止,我在django中得到了最奇怪的错误:
'if' statement improperly formatted
Run Code Online (Sandbox Code Playgroud)
引发错误的模板是这样的:
{% if diff >= 0 %}
<span class="pos">+{{ diff }}
{% else %}
<span class="neg">-{{ diff }}
{% endif %}
</span>
<span>{{ a }}</span>
Run Code Online (Sandbox Code Playgroud)
在上下文中有一个和diff的视图是这样的:
def add(request, kaart_id):
if request.is_ajax() and request.method == 'POST':
x = Kaart.objects.get(id=kaart_id)
x.pos += 1
x.save
x = Kaart.objects.get(id=kaart_id)
from django.utils import simplejson
diff = x.pos - x.neg
a = "(+1)"
context = { 'diff':diff, 'a':a }
return render_to_response('sum.html', context, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
在if,>,> =,==中使用哪个等式并不重要.它们都会引发相同的错误.
据我所知,我可以通过这本书告诉所有人:http://docs.djangoproject.com/en/dev/ref/templates/builtins/#id5
艾伦.
直到Django 1.2登陆,你正在寻找一个Django模板标签"Smart If".
django模板的更智能的{%if%}标记.
在保留当前Django功能的同时,它还处理相等,大于和小于运算符.一些常见案例::
Run Code Online (Sandbox Code Playgroud){% if articles|length >= 5 %}...{% endif %} {% if "ifnotequal tag" != "beautiful" %}...{% endif %}参数和运算符之间必须有空格,因此
{% if 1>2 %}不是有效的智能标记.所有支持的运营商有:
or,and,in,=(或==)!=,>,>=,<和<=.
| 归档时间: |
|
| 查看次数: |
14771 次 |
| 最近记录: |