• Flask基础1


    from flask import Flask,render_template,redirect,jsonify,send_file,request,Markup,session
    app = Flask(__name__)
    
    app.secret_key = "yinjiaodawangba"
    
    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.template_global()
    def ab(a,b):
        return a+b
    
    @app.template_filter()
    def axb(a,b):
        return a*b
    
    @app.route("/")
    def index():
        inp = Markup("<input type='text'>")
        if session.get("user"):
            print(session)
            # return json.dumps({"name":"JWB","age":73})
            # return jsonify({"name":"JWB","age":73})
            return render_template("index.html",stu=STUDENT_DICT,inp=inp)
        
        return redirect("/login")
    
    
    @app.route("/bl")
    def bl():
        # return json.dumps({"name":"JWB","age":73})
        # return jsonify({"name":"JWB","age":73}
        
        return render_template("my_block.html", stu=STUDENT_DICT)
    
    
    @app.route("/login",methods=("GET","POST"))
    def login():
        if request.method == "GET":
            return render_template("login.html")
        if request.method == "POST":
            user_info = request.form.to_dict()
            if user_info.get("username") == "JWB" and user_info.get("pwd") == "DSB":
                session["user"] = user_info.get("username")
                return redirect("/")
            else:
                return render_template("login.html",msg="用户名密码错误")
            
        
    
    if __name__ == '__main__':
        app.run(host="0.0.0.0",port=9527,debug=True)
    
    
    # eyJ1c2VyIjoiSldCIn0.DvS9eg.aXNffcXrztlPEzfR8ulOlU3Xbww
    # .eJyrViotTi1SslLyCndS0gFzDFF4Rig8YxSeCQrPFMqrBQDoaBcs.DvS9vQ.sW0iFbfMJmAz8kOZJ36aWRclY0c
    # .eJxFTTsSAiEMvYpDbUFCwsfS0gvY0MDCHkCHyvHuJsjONm9e8n4fM979ZW7m8byb6zxArjwYKAp2aIIINQ_qUE4X_jP6RrEky24p7lB8wCJhR2UpdGY2VRIm5XaW26RD1i8vH97gp7e1LpwrCt9tuUh99ZRHrG2OBJCPRRZeOWphAUWS8pC2OcG7-f4AmopIXQ.DvS-LA.lrKb_t7s7TVnDlwICSYEANSonJ8

    jinja2和模板语言的用法一样

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta http-equiv="content-Type" charset="UTF-8">
        <meta http-equiv="x-ua-compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>学生信息</title>
    </head>
    <body>
    {% include "my_block.html" %}
        {{ stu }}
    <table border="2px">
        <thead>
            <tr>
                <th>id</th>
                <th>name</th>
                <th>age</th>
                <th>gender</th>
            </tr>
        </thead>
        <tbody>
        {% for id,student in stu.items() %}
            <tr>
                <td>{{ id }}</td>
                <td>{{ stu[id].name }}</td>
                <td>{{ stu.get(id)["age"] }}</td>
                {% if stu[id]["gender"] != "" and stu[id]["gender"] != "" %}
                    <td>女</td>
                {% else %}
                    <td>{{ stu[id]["gender"] }}</td>
                {% endif %}
    
            </tr>
        {% endfor %}
    
        </tbody>
    </table>
    {{ inp}}
    {{ ab(6,9) }}
    {{ ab(3,4) | axb(834473503) }}
    
    {% macro my_input(na,tp) %}
        <input type="{{ tp }}" value="{{ na }}">
    {% endmacro %}
    
    {{ my_input("uname","submit") }}
    
    {% block content %}
    
    {% endblock %}
    
    </body>
    </html>
  • 相关阅读:
    分数的加减法——C语言初学者代码中的常见错误与瑕疵(10)
    关于陈冰、陈良乔以及《我的第一本C++书》【转】
    逻辑训练与说理教育从小学开始【转】
    薛非《品悟C-抛弃C程序设计中的谬误与恶习》读后感part1【转】
    C语言初学者代码中的常见错误与瑕疵(9)
    为什么在我眼里你是一只傻逼——傻逼“常所用”句型之(1)——“就算……但是……”
    要心中有“数”——C语言初学者代码中的常见错误与瑕疵(8)
    220v转5v阻容降压电路
    IAR for stm8 memory窗口的功能
    stm8 io口重映射
  • 原文地址:https://www.cnblogs.com/shuai1993/p/10151678.html
Copyright © 2020-2023  润新知