if/for django

4510 단어
ifequal
Output the contents of the block if the two arguments equal each other.
Example:
{% ifequal user.id comment.user_id %}     ...{% endifequal %} 

As in the if tag, an {% else %} clause is optional.
The arguments can be hard-coded strings, so the following is valid:
{% ifequal user.username "adrian" %}     ...{% endifequal %} 

It is only possible to compare an argument to template variables or strings. You cannot check for equality with Python objects such as True or False. If you need to test if something is true or false, use the if tag instead.
Boolean operators
if tags may use and, or or not to test a number of variables or to negate a given variable:
{% if athlete_list and coach_list %}     Both athletes and coaches are available.{% endif %} {% if not athlete_list %}     There are no athletes.{% endif %} {% if athlete_list or coach_list %}     There are some athletes or some coaches.{% endif %} {% if not athlete_list or coach_list %}     There are no athletes or there are some coaches (OK, so
    writing English translations of boolean logic sounds
    stupid; it's not our fault).{% endif %} {% if athlete_list and not coach_list %}     There are some athletes and absolutely no coaches.{% endif %} 

Changed in Django 1.2:  Please see the release notes
Use of both and and or clauses within the same tag is allowed, with and having higher precedence than or e.g.:
{% if athlete_list and coach_list or cheerleader_list %} 

will be interpreted like:
if (athlete_list and coach_list) or cheerleader_list

Use of actual brackets in the if tag is invalid syntax. If you need them to indicate precedence, you should use nested if tags.
for ... empty
The for tag can take an optional {% empty %} clause that will be displayed if the given array is empty or could not be found:
<ul> {% for athlete in athlete_list %}     <li>{{ athlete.name }}</li> {% empty %}     <li>Sorry, no athlete in this list!</li> {% endfor %} <ul> 

The above is equivalent to -- but shorter, cleaner, and possibly faster than -- the following:
<ul>   {% if athlete_list %}     {% for athlete in athlete_list %}       <li>{{ athlete.name }}</li>     {% endfor %}   {% else %}     <li>Sorry, no athletes in this list.</li>   {% endif %} </ul>

 
 
표 F-1.{% for%} 순환 중 사용 가능한 변수
변수 이름
묘사
forloop.counter
현재 순환 횟수 (인덱스 최소 1).
forloop.counter0
현재 순환 횟수 (인덱스 최소 0).
forloop.revcounter
남은 순환 횟수 (인덱스 최소 1).
forloop.revcounter0
남은 순환 횟수 (인덱스 최소 0).
forloop.first
첫 번째 주기는 True입니다.
forloop.last
마지막 루프는 트루입니다.
forloop.parentloop
네스트된 주기에 사용되며 현재 주기 바깥쪽의 주기를 나타냅니다.

좋은 웹페이지 즐겨찾기