django template if or statement

前端 未结 1 1375
无人共我
无人共我 2021-02-11 14:44

Basically to make this quick and simple, I\'m looking to run an XOR conditional in django template. Before you ask why don\'t I just do it in the code, this isn\'t an option.

1条回答
  •  攒了一身酷
    2021-02-11 15:15

    and has higher precedence than or, so you can just write the decomposed version:

    {% if user.username in req.accepted.all and user.username not in req.declined.all or
          user.username not in req.accepted.all and user.username in req.declined.all %}
    

    For efficiency, using with to skip reevaluating the querysets:

    {% with accepted=req.accepted.all declined=req.declined.all username=user.username %}
        {% if username in accepted and username not in declined or
              username not in accepted and username in declined %}
        ...
    {% endif %}
    {% endwith %}
    

    0 讨论(0)
提交回复
热议问题