zoukankan      html  css  js  c++  java
  • flask 2 进阶

    # 创建项目

    jinja2 语法基础

    # pycharm 里面 创建 new project -->pure  python 之后选择路径 选择解释器 以及虚拟环境问题
    
    from flask import Flask,request,redirect,render_template,jsonify,send_file
    app = Flask(__name__)
    
    
    STUDENT = {'name': 'Old', 'age': 38, 'gender': ''}
    
    STUDENT_LIST = [
        {'name': 'Old', 'age': 38, 'gender': ''},
        {'name': 'Boy', 'age': 73, 'gender': ''},
        {'name': 'EDU', 'age': 84, 'gender': ''}
    ]
    
    STUDENT_DICT = {
        1: {'name': 'Old', 'age': 38, 'gender': ''},
        2: {'name': 'Boy', 'age': 73, 'gender': ''},
        3: {'name': 'EDU', 'age': 84, 'gender': ''},
    }
    
    @app.route('/') #根目录
    def index():
        return 'Hello Life'
    
    @app.route('/stu') #基本字典
    def stu():
        return render_template('index.html',student=STUDENT)
    
    @app.route('/stu1') #列表
    def stu1():
        return render_template('index1.html',student=STUDENT_LIST)
    
    @app.route('/stu2') #字典
    def stu2():
        return render_template('index2.html',student=STUDENT_DICT)
    
    if __name__ == '__main__':
        app.run('0.0.0.0',6900,debug=True)
    一个主py文件
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Old Boy EDU</title>
    </head>
    <body>
    <div> _____________________________________</div>
    Welcome to Old Boy EDU : student
    <div>{{ student }}</div>
    <table border="1px">
        <tr>
            <td>{{ student.name }}</td>
            <td>{{ student["age"] }}</td>
            <td>{{ student.get("gender") }}</td>
        </tr>
    </table>
    <div> _____________________________________</div>
    Welcome to Old Boy EDU : student_list
    <div>{{ student_list }}</div>
    <table border="1xp">
        {% for foo in student_list %}
            <tr>
                <td>{{ foo }}</td>
                <td>{{ foo.name }}</td>
                <td>{{ foo.get("age") }}</td>
                <td>{{ foo["gender"] }}</td>
            </tr>
        {% endfor %}
    </table>
    <div> _____________________________________</div>
    Welcome to Old Boy EDU : student_dict
    <div>{{ student_dict }}</div>
    <table border="1xp">
        {% for foo in student_dict %}
            <tr>
                <td>{{ foo }}</td>
                <td>{{ student_dict.get(foo).name }}</td>
                <td>{{ student_dict[foo].get("age") }}</td>
                <td>{{ student_dict[foo]["gender"] }}</td>
            </tr>
        {% endfor %}
    </table>
    </body>
    </html>
    配合的前端代码 整合

    jinja2 高阶

    safe : 此时你与HTML只差一个 safe
    后端
    from flask import Flask
    from flask import render_template
    app = Flask(__name__)
    @app.route("/")
    def index():
        tag = "<input type='text' name='user' value='DragonFire'>"
        return render_template("index.html",tag=tag)
    app.run("0.0.0.0",5000)
  • 相关阅读:
    Mysql update case
    phpexcel导出excel等比例缩放图片
    phpexcel错误 You tried to set a sheet active by the out of bounds index: 1解决办法
    phpexcel操作
    Java io基础
    java线程基础
    java 集合基础(适用单线程)
    java 泛型深入
    Java反射基础
    Java泛型基础
  • 原文地址:https://www.cnblogs.com/zhangchen-sx/p/11006945.html
Copyright © 2011-2022 走看看