don*_*yor 1 python django django-templates
我想在django模板中这样做:
{% for book in books %}
if book in this library
print "already in this library"
else
print "add to this library"
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
有两个库L和O.
但同一本书可以在多个图书馆,我在图书馆L.这本书b在两个图书馆.这就是为什么我的循环给出两个输出.
already in this library
add to this library
Run Code Online (Sandbox Code Playgroud)
这是胡说八道.我该如何修复这个逻辑?
所以有两种情况:
我的建议是在视图级别而不是在模板级别执行此操作: