PYTHON framework-DJANGO Study Notes (7)
Eif/else {% if %} Label check (evaluate) a variable, if this variable is true (that is, the variable exists, not empty, not Boolean false ), the system displays any content between {% if %} and {% endif %}, for example, 1 {% if today_is_weekend %} 2 <p> Welcome to the weekend! </P> 3 {% endif %} {% else %} tags are optional: 1 {% if today_is_weekend %} 2 <p> Welcome to the weekend! </P> 3 {% else %} 4 <p> Get back to work. </p> 5 {% endif %} the "true value" of Python is in the Python and Django template systems. The following objects are equivalent to False of Boolean values: Empty list ([]) null tuples () null Dictionary ({}) Null String ('') zero value (0) special object None object False (obvious) everything except the preceding points is considered as "True ". The {% if %} tag accepts the and, or not keyword to judge multiple variables, or returns the inverse (not) of the variables. For example: copy code {% 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. {% endif % }{% If athlete_list and not coach_list %} There are some athletes and absolutely no coaches. the {% endif %} duplicate code {% if %} tag cannot use and or in the same tag because it may be fuzzy logically, for example, the following example is incorrect: for example, this code is invalid: {% if athlete_list and coach_list or cheerleader_list %} the system does not support combination and comparison with parentheses. If you really need to use parentheses to combine and express your logic, consider moving it out of the template for processing, and then pass the results in the form of template variables. Or replace it with the nested {% if %} tag, just like this: 1 {% if athlete_list %} 2 {% if coach_list or cheerleader_list %} 3 We have athletes, and either coaches or cheerleaders! 4 {% endif %} 5 {% endif %} It is okay to use the same logical operator multiple times, but we cannot combine different operators. For example, this is legal: {% if athlete_list or coach_list or parent_list or teacher_list %} does not have the {% elif %} tag, use the nested ''{% if %}'' tag to achieve the same effect: Copy code 1 {% if athlete_list %} 2 <p> Here are the athletes: {athlete_list }}. </p> 3 {% else %} 4 <p> No athletes are available. </p> 5 {% if coach_list %} 6 <p> Here are the coaches :{{ coach_list }}. </p> 7 {% endif %} 8 {% endif %} copy the code PS: Be sure to use {% endif %} to close every {% if %} tag. For {% for %} allows us to iterate over a sequence. Similar to the for statement in Python, the loop syntax is for X in Y, Y is the sequence to be iterated, and X is the variable name used in each specific loop. In each loop, the template system renders all content between {% for %} and {% endfor %. For example, given an athlete list athlete_list variable, we can use the following code to display this list: 1 <ul> 2 {% for athlete in athlete_list %} 3 <li >{{ athlete. name }}</li> 4 {% endfor %} 5 </ul> adds a reversed to the tag so that the list is reversely iterated: {% for athlete in athlete_list reversed % }... {% endfor %} can be nested with the {% for %} Tag: Copy code 1 {% for athlete in athlete_list %} 2