flask control statement

All control statements are placed in {%… %}, and there is a statement {% endxxx %} to end, the commonly used control statements in Jinja are if/for…in…

1. Condition control: if statement

  • The if statement is similar to that in python, you can use >, <, <=, >=, ==, != to make judgments, and you can also use and, or, not, () to perform logical merging operations
from flask import Flask,render_template

app=Flask(__name__)

context={
    
    
    'username': '老萝卜'
}

@app.route("/")
def if_for():
    return render_template('demo_flaskif.html', **context)


if __name__=="__main__":
    app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flask if控制语句演示</title>
</head>
<body>
    {
    
    %if username=="老萝卜"%}
    <p>当前用户是:{
    
    {
    
    username}}</p>
    {
    
    %else%}
     <p>当前用户不是:老萝卜,而是{
    
    {
    
    username}}</p>
    {
    
    %endif%}

</body>
</html>

Insert picture description here
Insert picture description here
Change username to another value,'username':'laoluobo'
Insert picture description here
Insert picture description here

2. Loop control: for...in...

  • The for loop can traverse any sequence including lists, dictionaries, and tuples. And can be reverse traversed
  • If there is no value in the sequence, enter else
  • And the for loop in Jinja also contains the following variables, which can be used to get the current traversal status
variable description
loop.index The index of the current iteration (starting from 1)
loop.index0 The index of the current iteration (starting from 0)
loop.first Whether it is the first iteration, return True or False
loop.last Whether it is the last iteration, return True or False
loop.length The length of the sequence
  • In addition, you cannot use continue and break expressions to control the execution of the loop.
from flask import Flask,render_template

app=Flask(__name__)

context={
    
    
    'books': ['Python', 'Java', 'PHP'],
    'book': {
    
    'Python': 666,
             'Java': 777,
             'PHP': 888
             },
    "netebooks":[]
}

@app.route("/")
def flask_for():
    return render_template('demo_flaskfor.html', **context)

if __name__=="__main__":
    app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flask if控制语句演示</title>
</head>
<body>
    <p> 循环显示列表 books:</p>
    {
    
    % for book in books %}          <!--循环显示列表-->
        <p>{
    
    {
    
     book }}</p>
    {
    
    % else %}
        <p>列表books为空列表</p>
    {
    
    % endfor %}
    <hr>

    <p> 循环显示列表 notebooks:</p>
    {
    
    % for book in notebooks %}          <!--循环显示空列表-->
        <p>{
    
    {
    
     book }}</p>
     {
    
    % else %}
        <p>列表notebooks为空列表</p>
    {
    
    % endfor %}
    <hr>

    <p>循环显示字典:</p>
    {
    
    % for i in book %}                 <!--循环显示字典-->
        <p>{
    
    {
    
     i }}</p>
    {
    
    % endfor %}
    <hr>

    <p>循环显示字典的key和value</p>
    {
    
    % for key, value in book.items() %}    <!--循环显示字典的key和value -->
        <p>{
    
    {
    
     key }} -- {
    
    {
    
     value }}</p>
    {
    
    % endfor %}

    <hr>
    <p>显示循环当前遍历状态 </p>
    {
    
    % for book in books %}
        <p>{
    
    {
    
     book }}</p>
        <p>当前迭代的索引(从0开始):{
    
    {
    
     loop.index0 }}</p>
        {
    
    % if loop.first %}
            <p>是否是第一次迭代,返回TrueFalse{
    
    {
    
     loop.first }}</p>
        {
    
    % endif %}
        {
    
    % if loop.last %}
            <p>是否是最后一次迭代,返回TrueFalse{
    
    {
    
     loop.last }}</p>
        {
    
    %endif%}
    {
    
    % endfor %}

</body>
</html>

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/laoluobo76/article/details/109111247