Flask learning (3)-the basic use of variable code blocks, control code blocks, and filters

app.py

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def hello_world():
    url_str = 'https://www.baidu.com'
    my_list = [11, 22, 33, 44, 55]
    my_dict = {
    
    
        'name': 'James',
        'age': 36
    }
    my_int = 18

    return render_template('index.html', url_str=url_str, my_list=my_list, my_dict=my_dict, my_int=my_int)


if __name__ == '__main__':
    app.run(host='192.168.235.128', port=5000, debug=True)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>学习了</h1> <br>
{
    
    # 一个变量代码块的使用 #}
{
    
    {
    
     url_str }} <br>

{
    
    # 列表的使用 #}
{
    
    {
    
     my_list }} <br>
{
    
    {
    
     my_list.2 }} <br>
{
    
    {
    
     my_list[2] }} <br>

{
    
    # 字典的使用 #}
{
    
    {
    
     my_dict }} <br>
{
    
    {
    
     my_dict.name }} <br>
{
    
    {
    
     my_dict['name'] }} <br>

{
    
    {
    
     my_int }} <br>

{
    
    # for循环的使用,可以先敲for,然后按住tab进行补全#}
{
    
    % for num in my_list %}
    {
    
    {
    
     num }} <br>
    {
    
    % if num>50 %}
       {
    
    {
    
     '大数字' }}
    {
    
    % endif %}
{
    
    % endfor %}
<hr>
{
    
    # 过滤器 #}

{
    
    # 字符串变大写 #}
{
    
    {
    
     url_str | upper }} <br>
{
    
    # 字符串反转 #}
{
    
    {
    
     url_str | reverse }} <br>
{
    
    # 字符串链式调用 #}
{
    
    {
    
     url_str | upper | reverse | lower}} <br>


</body>
</html>

Run results:
Insert picture description here
commonly used filters:

  • abs: absolute value
  • default: If the current variable has no value, the value in the parameter will be used instead
  • escape: escape character
  • first: returns the first element of a sequence
  • format: format string
  • last: returns the last element of a sequence
  • length: returns the length of a sequence
  • join: concatenate strings
  • safe: turn off escaping
  • int: converted to int type
  • float: convert to floating point type
  • lower: convert to lowercase
  • upper: convert to thank you
  • replace: replace
  • truncate: intercept a string of length length
  • striptags: delete all html tags in the string, if there are multiple spaces, they will be replaced with one space

Guess you like

Origin blog.csdn.net/qq_34663267/article/details/111876150